0

I have a dataframe of games played by different number of players for each game. Simplified, it looks like this:

GameID Player Score
------ ------ -----
1      John   10
1      Alice  20
1      Bob    30
2      John   15
2      Alice  25

I'm trying to display the dataframe of the winner of each game but I'm having trouble with the syntax after using groupby. I managed to get a Series or a Dataframe of the rows with the highest score in each game by doing either of these:

df.groupby('GameID')['Score'].max()
df.groupby('GameID').aggregate({'Score': max})

But I'd like to display the entire dataframe, including the Player column (as well as other columns present in the dataframe, which I've simplified here).

What is the correct way to do this?

Cirrocumulus
  • 520
  • 3
  • 15
  • The title could be clearer. At first I thought it was going to be a duplicate of [How to print a groupby object](/q/22691010/4518341), but really you're asking something more like, "How do I get all the columns of a dataframe after aggregating on one column in a groupby?" For more info, [ask] has tips on how to write a good title. – wjandrea Feb 18 '23 at 20:09
  • Related: [Keep other columns when using sum() with groupby](/q/49783178/4518341) – wjandrea Feb 18 '23 at 20:37
  • Thanks. I've modified the title but I can't understand from the linked question how I should proceed. I need to display the single row in the `groupby` object which has the highest value of 'Score' in that group. – Cirrocumulus Feb 18 '23 at 20:56
  • The top answer was kinda unclear, so I edited it. Using that technique, you'd do `idx = df.groupby('GameID')['Score'].transform(max) == df['Score']` then `df[idx]`. – wjandrea Feb 18 '23 at 21:19
  • Thanks a lot. I'll look into the `transform` method to see why this works. – Cirrocumulus Feb 19 '23 at 06:28

0 Answers0