df1 is DataFrame with 4 columns.
I want to created a new DataFrame (df2) by grouping df1 with Column 'A' with multi-column operation on column 'C' and 'D'
Column 'AA' = mean(C)+mean(D)
Column 'BB' = std(D)
df1= 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)})
A B C D
0 foo one 1.652675 -1.983378
1 bar one 0.926656 -0.598756
2 foo two 0.131381 0.604803
3 bar three -0.436376 -1.186363
4 foo two 0.487161 -0.650876
5 bar two 0.358007 0.249967
6 foo one -1.150428 2.275528
7 foo three 0.202677 -1.408699
def fun1(gg): # this does not work
return pd.DataFrame({'AA':C.mean()+gg.C.std(), 'BB':gg.C.std()})
dg1 = df1.groupby('A')
df2 = dg1.apply(fun1)
This does not work. It seems like aggregation() only works for Series and multi-column operation is not possible. And apply() only produce Series output with multi-column operation. Is there any other way to produce multi-column output (DataFrame) with multi-column operation?