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

Time:12-17

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?

CodePudding user response:

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

CodePudding user response:

don't store them in variable maximum_score .beacuse doing this you are modifying your dataframe to one column only just called df in next line and you'll have your result

#maximum calcualtion
df.groupby(['Year'])['Point'].max()
df//print(df)
  • Related