1

I have pets dataFrame.

I can do:

df=pets['PetID'].groupby([pets['Kind'], pets['Gender']]).count()

The result of dataframe(the variable df) is:

Kind    Gender
Cat     female    12
        male      19
Dog     female    22
        male      35
Parrot  female     7
        male       5
Name: PetID, dtype: int64

I want get the most popular Gender of Pets groupby "Kind", I want to get result like:

Kind   Most_Gender
Cat    male
Dog    male
Parrot female

How should I do for the data

user504909
  • 9,119
  • 12
  • 60
  • 109
  • 1
    Please add your `df` before the groupby as well, so we can reproduce the problem ourself. – Erfan Nov 25 '19 at 14:56
  • @Erfan My pets dataframe you can see over 100 lines, How to add the whole pets dataframe? for df is already show. – user504909 Nov 25 '19 at 15:01
  • Normally it's better to show your problem with an example dataframe which represents your original data, here are some good [answers](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) which specify how to achieve this. But for now I think WenYoBen's answers should help you – Erfan Nov 25 '19 at 15:04

1 Answers1

0

In your case we can use mode

pets['Gender'].groupby(pets['Kind']).apply(lambda x : x.mode().iloc[0])

To fix your output df

df.sort_values().groupby(level=0).tail(1).reset_index()
BENY
  • 317,841
  • 20
  • 164
  • 234