Categories
Python Answers

How to do aggregation in Python Pandas?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *