0

I have the following dataframe

      medal      number Age 
      Gold        5     25
      Silver      4     30
      Bronze      3     45
      Gold        1     23
      Silver      2     12
      Bronze      3     16

And, I am trying to groupby on medal and get sum of 'number' and mean of 'Age'. I can do it in two lines but how to do it single line with pandas groupby.

I can do 1 operation at a time with

df.groupby(['medal'])['Age'].mean()

or

df.groupby(['medal'])['number'].sum()

And then merge maybe, Which is a long process. So how to achieve it in pandas way

Below is the desired output

       medal   number   Age
      Bronze     6      30.5
      Gold       6      24.0
      Silver     6      21.0
Manu Sharma
  • 1,593
  • 4
  • 25
  • 48

2 Answers2

3

Aggregate by dictionary for columns names with aggregate functions and add as_index=False for column from medal in output DataFrame:

df1 = df.groupby('medal', as_index=False).agg({'Age': 'mean','number':'sum'})
print (df1)
    medal   Age  number
0  Bronze  30.5       6
1    Gold  24.0       6
2  Silver  21.0       6
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Use the agg-method on groupby with a dictionary, like so:

df.groupby(['medal']).agg({'number': 'sum', 'Age':'mean'})
Jondiedoop
  • 3,303
  • 9
  • 24