To do aggregation in Python Pandas, we can use groupby
and aggregeation methods.
For instance, we write
df1 = df.groupby(['A', 'B'], as_index=False)['C'].sum()
to get the sums of column A and B values in column C by call groupby
to group the values in the columns and then call sum
to sum up the grouped values.
We can also use agg
after groupby
to do aggregation.
For instance, we write
df5 = df.groupby(['A', 'B']).agg(['mean','sum'])
to call groupby
to do the same grouping and call agg
to return the mean and sum.