Skip to content Skip to sidebar Skip to footer

How To Apply Different Functions To Each Group Of Pandas Groupby?

If I have a dataframe as follows, import numpy as np import pandas as pd df2 = pd.DataFrame({'type':['A', 'A', 'B', 'B', 'C', 'C'], 'value':np.random.randn(6)}) >>> df2

Solution 1:

Upvoted abarnert's answer, because it's a good one.

On the other hand, in order answer OP's question according to OP's specification:

for group in df2.groupby('type'):
    print group
    if group[0] == 'A':
        print group[1].min()
    if group[0] == 'B':
        print group[1].max()
    if group[0] == 'C':
        print group[1].mean()

On the other hand, I would recommend simply computing everything for every group, since it's easy enough anyways. This is the intent behind doing a groupby operation.

In [5]: summary = pd.DataFrame()

In [6]: summary['mean'] = df2.groupby('type').mean()['value']

In [7]: summary['min'] = df2.groupby('type').min()['value']

In [8]: summary['max'] = df2.groupby('type').max()['value']

summary will look like this:

In [9]: summary
Out[9]: 
          mean       min       max
type                              
A     0.440490  0.231633  0.649346
B     0.172303  0.023094  0.321513
C     0.669650 -0.373361  1.712662

Solution 2:

Why even use groupby here? It's just getting in the way, and you don't want to do anything with the groups as a whole. So why not just select each group manually?

>>> df2[df2.type=='A']['value'].min()
-1.4442888428898644
>>> df2[df2.type=='B']['value'].max()
1.0361392902054989
>>> df2[df2.type=='C']['value'].mean()
0.89822391958453074

Post a Comment for "How To Apply Different Functions To Each Group Of Pandas Groupby?"