Reputation: 47
I have Pandas df:
family age fruits
------------------
Brown 12 7
Brown 33 5
Yellow 28 3
Yellow 11 9
I want to get ages with next conditions:
So result df will be:
family age
-----------
Brown 12
Yellow 11
Upvotes: 1
Views: 40
Reputation: 323306
Use head
after sort_values
df.sort_values(
['family','fruits'], ascending=[True,False])
.groupby('family').head(1)
Upvotes: 1
Reputation: 150765
We can do:
(df.sort_values(['family','fruits'], ascending=[True,False])
.drop_duplicates('family')
)
Output:
family age fruits
0 Brown 12 7
3 Yellow 11 9
Or with groupby().idxmax()
df.loc[df.groupby('family').fruits.idxmax(), ['family','age'] ]
Output:
family age
0 Brown 12
3 Yellow 11
Upvotes: 3