简体   繁体   中英

groupby in pandas with different functions for different columns

Best to illustrate by example:

I would like to aggregate a DataFrame by col1 and col2 , summing results on col3 and col4 and averaging results on col5

If I just wanted to sum on col3-5 I'd use df.groupby(['col1','col2']).sum()

You can use the Groupby.agg() (or Groupby.aggregate() ) method for this.

aggregate() function can accept a dictionary as argument, in which case it treats the keys as the column names and the value as the function to use for aggregating. As given in the documentation -

By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame.

Example -

import numpy as np
result = df.groupby(['col1','col2']).agg({'col3':'sum','col4':'sum','col5':np.average})

Demo -

In [50]: df = pd.DataFrame([[1,2,3,4,5],[1,2,6,7,8],[2,3,4,5,6]],columns=list('ABCDE'))

In [51]: df
Out[51]:
   A  B  C  D  E
0  1  2  3  4  5
1  1  2  6  7  8
2  2  3  4  5  6

In [52]: df.groupby(['A','B']).aggregate({'C':np.sum,'D':np.sum,'E':np.average})
Out[52]:
     C    E   D
A B
1 2  9  6.5  11
2 3  4  6.0   5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM