简体   繁体   中英

Add new column to grouped object in python pandas

I am trying to do the following: I have a data frame consisting of four columns. I group the frame by column0, then call a function on the grouped object to add an additional column in each group. However, when I try calling the function on the grouped object, I get an Error. My code looks like this:

def function(x):
    return x['column2']-x['column1']


Groupeddf = df.groupby('column0')
for key, group in Groupeddf: 
    Nullgroup=group[group.column3 == 0]
    Nullgroup['new_column']=Nullgroup.apply(function, axis=1)

When I try the code, I get ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

My data frame is of the following type:

    column0  column1  column2  column3
0     a         2        5       1
1     a         3        7       0
2     b         1        3       4
3     c         3        5       0

Does anyone have an idea how to fix this?

You can try groupby with custom function f with loc :

def f(x):
    x.loc[x.column3 == 0, 'new_column']  =  x['column2'] - x['column1']
    return x

print df.groupby('column0').apply(f)  
  column0  column1  column2  column3  new_column
0       a        2        5        1         NaN
1       a        3        7        0           4
2       b        1        3        4         NaN
3       c        3        5        0           2  

But maybe groupby can be omit:

df.loc[df.column3 == 0, 'new_column']  =  df['column2'] - df['column1']
print df
  column0  column1  column2  column3  new_column
0       a        2        5        1         NaN
1       a        3        7        0           4
2       b        1        3        4         NaN
3       c        3        5        0           2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM