Reputation: 21533
My dataframe
has a column contains various type values, I want to get the most counted one:
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
Reputation: 6383
Use argmax
:
lbl = df['type'].value_counts().argmax()
To query,
df.query("type==@lbl")
Upvotes: 5