简体   繁体   中英

What´s the difference between idxmax() and max() inside a groupby pandas

I have a dataframe with data from many players of different teams. What I wanted was to groupby 'team' and only keep the rows with the max value from height.

When I used this code, I just got the 'Team' label and peoples heights:

Code:

df.groupby(['team'], sort=True)['height'].max()

Output:

team
Argentina 199
Australia 193
Belgium 199 (...)

Then I combined the groupby with loc to get the whole information of the players:

fifa.loc[fifa.groupby('team')['height'].idxmax()]

This printed the whole rows, but I don´t understand why do I use idxmax() and not max() at the end of the code.

Can anyone explain me the difference?

max() simply returns the maximum value.

idmax() returns the index of the (first occurrence of the) maximum value, not the maximum value itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM