Reputation: 899
I have a dataframe df
. I need to group by column a
, find max of column b
but want to display all the columns in df
as well.
For example -
df:
a b c d
0 259992 20 55 abc
1 259992 10 12 def
2 1912687 11 11 ghi
My code to find max of b
:
df.groupby('a')['b'].max().reset_index()
This gives me a partial result:
a b
0 259992 20
1 1912687 11
What I want to obtain is:
a b c d
0 259992 20 55 abc
1 1912687 11 11 ghi
Any tips on how to do this in the most efficient manner ?
Upvotes: 1
Views: 39
Reputation: 75080
Looks like you need idxmax
:
df.loc[df.groupby('a')['b'].idxmax()]
a b c d
0 259992 20 55 abc
2 1912687 11 11 ghi
Upvotes: 2