How to Run Multiple Aggregations on the same Column when using Pandas GroupBy

Running multiple aggregations when grouping a Pandas DataFrame can be accomplished using the .agg function and passing in a dictionary. In this post, I’ll show you how to do that.

To start, I’ll create a random DataFrame:

df = pd.DataFrame(np.random.randint(3,size=(4, 3)), index = ['apples','apples','oranges','oranges'], columns=['A','B','C'])
df

To apply more than one aggregation when using pandas GroupBy, you simply pass in a dictionary to the .agg function. In your dictionary, your key will be the column name and the value will be a list of operations you want to perform on the column.

The result will be a DataFrame with a MultiIndex column.

df_grouped = df.groupby(df.index).agg({'A':['sum','mean'],'B':'sum','C':'sum'})
df_grouped

Posted

in

by

Tags: