How to get Maximum points scored by which player in each year?

I have a Data set like this –

Name    Point Year

Player1 498.0 2010

Player2 454.0 2010

Player1 396.0 2011

Player3 214.0 2011

player2 163.0 2011

Now I want to see which Player scored the maximum point in each year.

I tried this –

Maximum_score = df.groupby(['Year'])['Point'].max()

and got the result –

Year

2010   498.0

2011   396.0

But I want player Name too. How to do this?

>Solution :

You can use boolean indexing. groupby on Year to find maximum points (like you already did) + transform max values for each group for every player in each group, and filter for the players who scored the maximum points for each group:

out = df[df.groupby(['Year'])['Point'].transform('max') == df['Point']]

Output:

      Name  Point  Year
0  Player1  498.0  2010
2  Player1  396.0  2011

Leave a Reply