Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify pandas group

I have a DataFrame, which I group. I would like to add another column to the data frame, that is a result of function diff, per group. Something like:

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
                         'foo', 'bar', 'foo', 'foo'],
                   'B' : ['one', 'one', 'two', 'three',
                          'two', 'two', 'one', 'three'],
                   'C' : np.random.randn(8),
                   'D' : np.random.randn(8)})
df_grouped = df.groupby('B')
for name, group in df_grouped:
   new_df["D_diff"] = group["D"].diff()

I would like to get per each group the differnece of column D, and have a DF that include a new column with the diff calculation.

like image 460
matlabit Avatar asked Aug 10 '16 09:08

matlabit


1 Answers

IIUC you can use DataFrameGroupBy.diff:

df['D_diff'] = df.groupby('B')['D'].diff()
print (df) 
     A      B         C         D    D_diff
0  foo    one  1.996084  0.580177       NaN
1  bar    one  1.782665  0.042979 -0.537198
2  foo    two -0.359840  1.952692       NaN
3  bar  three -0.909853  0.119353       NaN
4  foo    two -0.478386 -0.970906 -2.923598
5  bar    two -1.289331 -1.245804 -0.274898
6  foo    one -1.391884 -0.555056 -0.598035
7  foo  three -1.270533  0.183360  0.064007
like image 191
jezrael Avatar answered Oct 23 '22 10:10

jezrael