Pandas get the most frequent values of a column
i have this dataframe:
0 name data
1 alex asd
2 helen sdd
3 alex dss
4 helen sdsd
5 john sdadd
so i am trying to get the most frequent value or values(in this case its values) so what i do is:
dataframe['name'].value_counts().idxmax()
but it returns only the value: Alex even if it Helen appears two times as well.
By using mode
df.name.mode()
Out[712]:
0 alex
1 helen
dtype: object
To get the n
most frequent values, just subset .value_counts()
and grab the index:
# get top 10 most frequent names
n = 10
dataframe['name'].value_counts()[:n].index.tolist()
You could try argmax
like this:
dataframe['name'].value_counts().argmax()
Out[13]: 'alex'
The value_counts
will return a count object of pandas.core.series.Series
and argmax
could be used to achieve the key of max values.
df['name'].value_counts()[:5].sort_values(ascending=False)
The value_counts
will return a count object of pandas.core.series.Series
and sort_values(ascending=False)
will get you the highest values first.