To apply multiple functions to multiple groupby columns with Python Pandas, we can use the groupby
and agg
methods.
For instance, we write
df.groupby('group').agg(
a_sum=('a', 'sum'),
a_mean=('a', 'mean'),
b_mean=('b', 'mean'),
c_sum=('c', 'sum'),
d_range=('d', lambda x: x.max() - x.min())
)
to call agg
on the groups returned by groupby
with some arguments to computed aggregate values for various columns.
We compute the sum of columns in a
, the mean of a
and b
, the sum of c
and the differnce between the max and min columns in d
with agg
.