ZK Zhao
ZK Zhao

Reputation: 21533

Pandas: get the most count label

My dataframe has a column contains various type values, I want to get the most counted one:

enter image description here

In this case, I want to get the label FM-15, so later on I can query data only labled by this.

How can I do that?


Now I can get away with:

most_count = df['type'].value_counts().max()
s = df['type'].value_counts()
s[s == most_count].index

This returns

Index([u'FM-15'], dtype='object')

But I feel this is to ugly, and I don't know how to use this Index() object to query df. I only know something like df = df[(df['type'] == 'FM-15')].

Upvotes: 5

Views: 11448

Answers (1)

Happy001
Happy001

Reputation: 6383

Use argmax:

lbl = df['type'].value_counts().argmax()

To query,

df.query("type==@lbl")

Upvotes: 5

Related Questions