Do you have a typo in your f
function? Should AA
be C.mean() + C.std()
or C.mean() + D.mean()
In this first case, AA
= C.mean() + C.std()
,
In [91]: df = df1.groupby('A').agg({'C': lambda x: x.mean() + x.std(),
'D': lambda x x.std()})
In [92]: df
Out[92]:
C D
A
bar 1.255506 0.588981
foo 1.775945 0.442724
For the second one C.mean() + D.mean()
, things aren't quite as nice. When you give the .agg
function on groupby objects a dict, I don't think there's a way to get values from two columns.
In [108]: g = df1.groupby('A')
In [109]: df = pd.DataFrame({"AA": g.mean()['C'] + g.mean()['D'], "BB": g.std()['D']})
In [110]: df
Out[110]:
AA BB
A
bar 0.532263 0.721351
foo 0.427608 0.494980
You may want to assign g.mean() and g.std() to temporary variables to avoid calculating them twice.