Pandas get the most frequent values of a column

PythonPandasDataframe

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

By using mode

df.name.mode()
Out[712]: 
0     alex
1    helen
dtype: object

Solution 2 - Python

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()

Solution 3 - Python

You could try argmax like this:

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.

Solution 4 - Python

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.

Solution 5 - Python

You can use this to get a perfect count, it calculates the mode a particular column

df['name'].value_counts()

Solution 6 - Python

Here's one way:

df['name'].value_counts()[df['name'].value_counts() == df['name'].value_counts().max()]

which prints:

helen    2
alex     2
Name: name, dtype: int64

Solution 7 - Python

Use:

df['name'].mode()

or

df['name'].value_counts().idxmax()

Solution 8 - Python

Not Obvious, But Fast

f, u = pd.factorize(df.name.values)
counts = np.bincount(f)
u[counts == counts.max()]

array(['alex', 'helen'], dtype=object)

Solution 9 - Python

to get top 5:

dataframe['name'].value_counts()[0:5]

Solution 10 - Python

It will give top five most common names:

df['name'].value_counts().nlargest(5)

Solution 11 - Python

Simply use this..

dataframe['name'].value_counts().nlargest(n)

The functions for frequencies largest and smallest are:

  • nlargest() for mostfrequent 'n' values
  • nsmallest() for least frequent 'n' values

Solution 12 - Python

You could use .apply and pd.value_counts to get a count the occurrence of all the names in the name column.

dataframe['name'].apply(pd.value_counts)

Solution 13 - Python

To get the top five most common names:

dataframe['name'].value_counts().head()

Solution 14 - Python

my best solution to get the first is

 df['my_column'].value_counts().sort_values(ascending=False).argmax()

Solution 15 - Python

I had a similar issue best most compact answer to get lets say the top n (5 is default) most frequent values is:

df["column_name"].value_counts().head(n)

Solution 16 - Python

Identifying the top 5, for example, using value_counts

top5 = df['column'].value_counts()

Listing contents of 'top_5'

top5[:5]

Solution 17 - Python

n is used to get the number of top frequent used items

n = 2

a=dataframe['name'].value_counts()[:n].index.tolist()

dataframe["name"].value_counts()[a]

Solution 18 - Python

Getting top 5 most common lastname pandas:

df['name'].apply(lambda name: name.split()[-1]).value_counts()[:5]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalealeView Question on Stackoverflow
Solution 1 - PythonBENYView Answer on Stackoverflow
Solution 2 - PythonJared WilberView Answer on Stackoverflow
Solution 3 - PythonLunar_oneView Answer on Stackoverflow
Solution 4 - PythonTaieView Answer on Stackoverflow
Solution 5 - Pythonpaul okoduwaView Answer on Stackoverflow
Solution 6 - PythonpaultView Answer on Stackoverflow
Solution 7 - PythonMohit MehlawatView Answer on Stackoverflow
Solution 8 - PythonpiRSquaredView Answer on Stackoverflow
Solution 9 - PythonNaomi FridmanView Answer on Stackoverflow
Solution 10 - PythonSandhya KrishnanView Answer on Stackoverflow
Solution 11 - Pythonavineet07View Answer on Stackoverflow
Solution 12 - PythonBrianView Answer on Stackoverflow
Solution 13 - Pythonpedro_bb7View Answer on Stackoverflow
Solution 14 - PythonvenergiacView Answer on Stackoverflow
Solution 15 - PythonKZiovasView Answer on Stackoverflow
Solution 16 - PythonVictor SennaView Answer on Stackoverflow
Solution 17 - PythonHassan ButtView Answer on Stackoverflow
Solution 18 - PythonAlirezaView Answer on Stackoverflow