Andrey Yashin
Andrey Yashin

Reputation: 47

Pandas filter maximum groupby

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:

  1. Group by family;
  2. Having maximum of fruits

So result df will be:

family  age
-----------
Brown   12
Yellow  11

Upvotes: 1

Views: 40

Answers (2)

BENY
BENY

Reputation: 323306

Use head after sort_values

df.sort_values(
    ['family','fruits'], ascending=[True,False])
        .groupby('family').head(1)

Upvotes: 1

Quang Hoang
Quang Hoang

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

Related Questions