1

I have a dataframe as shown below

ID     Score
A      20
B      60
A      40
C      50
B      100
C      60
C      40
A      10
A      10
A      70

From the above I would like to calculate the average score for each ID and total score.

Expected output:

ID    Average_score     Total_score
A     30                150
B     80                160
C     50                150
Danish
  • 2,719
  • 17
  • 32

1 Answers1

2

Use named aggregation for custom columns names:

df1 = (df.groupby('ID').agg(Average_score=('Score','mean'), 
                            Total_score=('Score','sum'))
         .reset_index())
print (df1)
  ID  Average_score  Total_score
0  A             30          150
1  B             80          160
2  C             50          150
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252