Pandas groupby aggregate multiple columns

Do you want to know the details regarding “Pandas groupby aggregate multiple columns”. If yes, you’re in the correct tutorial.

Pandas groupby aggregate multiple columns

def f(x):
    d = 
    d['a_sum'] = x['a'].sum()
    d['a_max'] = x['a'].max()
    d['b_mean'] = x['b'].mean()
    d['c_d_prodsum'] = (x['c'] * x['d']).sum()
    return pd.Series(d, index=['a_sum', 'a_max', 'b_mean', 'c_d_prodsum'])

grouped_multiple = df.groupby(['Team', 'Pos']).agg('Age': ['mean', 'min', 'max'])
grouped_multiple.columns = ['age_mean', 'age_min', 'age_max']
grouped_multiple = grouped_multiple.reset_index()
df.groupby(['A','C'], as_index=False)['B'].sum()
df[['col1', 'col2', 'col3', 'col4']].groupby(['col1', 'col2']).agg(['mean', 'count'])
df['COUNTER'] =1       #initially, set that counter to 1.
group_data = df.groupby(['Alphabet','Words'])['COUNTER'].sum() #sum function


I hope this article helps you to know about “Pandas groupby aggregate multiple columns”. If you have any queries regarding this article please let us know via the comment section. Share this tutorial with your friends and family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment