Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible multiple function application with DataFrameGroupBy #642

Closed
wesm opened this issue Jan 17, 2012 · 1 comment
Closed

More flexible multiple function application with DataFrameGroupBy #642

wesm opened this issue Jan 17, 2012 · 1 comment

Comments

@wesm
Copy link
Member

wesm commented Jan 17, 2012

cc @arthurgerigk

Something of this nature would be nice:

df.groupby(['A', 'B']).agg({'C' : [np.mean, np.std]})

Would also be nice to add some options to agg for what to do with the input dict (by default targets columns)

@wesm
Copy link
Member Author

wesm commented May 14, 2012

Here's the outcome:

In [5]: df
Out[5]: 
     A      B         C         D
0  foo    one  0.727964  0.406402
1  bar    one  1.653679  0.266521
2  foo    two -1.677686 -1.143690
3  bar  three -0.591334 -0.594644
4  foo    two -0.839865  0.153575
5  bar    two -0.857455 -0.054324
6  foo    one -0.148167  1.080535
7  foo  three  0.118469  0.715423

In [6]: grouped = df.groupby('A')

In [7]: result = grouped.aggregate({'C' : [np.mean, np.std],
   ...:                                     'D' : [np.mean, np.std]})

In [8]: result
Out[8]: 
            C                   D          
         mean       std      mean       std
A                                          
bar  0.068297  1.379414 -0.127482  0.435219
foo -0.363857  0.925198  0.242449  0.848860

In [9]: grouped.aggregate({'C' : np.mean, 'D' : [np.mean, np.std]})
Out[9]: 
            C         D          
         mean      mean       std
A                                
bar  0.068297 -0.127482  0.435219
foo -0.363857  0.242449  0.848860

In [10]: paste
        result = grouped.aggregate({'C' : np.mean,
                                    'D' : {'foo': np.mean,
                                           'bar': np.std}})
## -- End pasted text --

In [11]: result
Out[11]: 
            C         D          
         mean       foo       bar
A                                
bar  0.068297 -0.127482  0.435219
foo -0.363857  0.242449  0.848860

@wesm wesm closed this as completed in 851afe5 May 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant