python - Get the row(s) which have the max value in groups using groupby - Stack Overflow

PHOTO EMBED

Tue Feb 08 2022 09:40:48 GMT+0000 (Coordinated Universal Time)

Saved by [deleted user] #python

In [3]: idx = df.groupby(['Mt'])['count'].transform(max) == df['count']

In [4]: df[idx]
Out[4]:
    Sp  Mt Value  count
0  MM1  S1     a      3
3  MM2  S3    mk      8
4  MM2  S4    bg     10
8  MM4  S2   uyi      7
content_copyCOPY

https://stackoverflow.com/questions/15705630/get-the-rows-which-have-the-max-value-in-groups-using-groupby