Most frequent value in a Pandas Column

In this tutorial, we will look at how to get the most frequent value in pandas column with the help of some examples.

most frequent value in pandas column

The most frequent value in a pandas series is basically the mode of the series. You can get the mode by using the pandas series mode() function. The following is the syntax:

# get mode of a pandas column
df['Col'].mode()

It returns the modes of the series in sorted order.

You can also use the pandas value_counts() function with the idxmax() function to return the value with the highest count. The following is the syntax:

# most frequent value in a pandas column
df['Col'].value_counts().idxmax()

Let’s look at some examples of getting the mode in a pandas column.

First, let’s create a dataframe with a categorical field that we will be using throughout this tutorial.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'Name': ['Steve', 'Varun', 'Maya', 'Jones', 'Emily', 'Stuart', 'Karen'],
    'Team': ['Red', 'Blue', 'Blue', 'Red', 'Green', 'Green', 'Blue']
})
# display the dataframe
print(df)

Output:

     Name   Team
0   Steve    Red
1   Varun   Blue
2    Maya   Blue
3   Jones    Red
4   Emily  Green
5  Stuart  Green
6   Karen   Blue

The dataframe df stores the names and the team information of students for a science project. The column “Team” is a categorical field with values representing the team assigned to the corresponding student.

Mode is a descriptive statistic that is equal to the most frequent value in the dataset. Let’s apply the pandas series mode() function to get the most frequent value in the “Team” column, which essentially tells us which team has the most students.

# most frequent value in Team
df['Team'].mode()

Output:

0    Blue
dtype: object

It returns a pandas series with the mode of the column. You can see that we get “Blue” as the mode since it is the most frequent value in the “Team” column.

Note that you can also apply the mode() function on a pandas dataframe to get the mode of each column.

The pandas value_counts() function is used to get the count of each unique value in a pandas series. You can use it to get the counts and then extract the value with the most counts using idxmax() function. For example –

# most frequent value in Team
df['Team'].value_counts().idxmax()

Output:

'Blue'

We get the value with the highest value count in the “Team” column.

Note that this method only gives a single value as output even if there are more than one modes present.

Let’s find out what the above two methods give when we have a tie for the most frequent value.

For this, let’s modify the dataframe so that we have two modes in the “Team” column. Here we modify the “Team” value for “Jones” from “Red” to “Green”.

# change Jones' team to Green
df.at[3, 'Team'] = 'Green'
# display the dataframe
print(df)

Output:

     Name   Team
0   Steve    Red
1   Varun   Blue
2    Maya   Blue
3   Jones  Green
4   Emily  Green
5  Stuart  Green
6   Karen   Blue

You can see that now we have two modes – “Blue” and “Green” both occurring three times in the “Team” column.

Now, let’s find the mode of the “Team” with the mode() function.

# most frequent value in Team
df['Team'].mode()

Output:

0     Blue
1    Green
dtype: object

We get both the modes in the returned series.

Let’s check what we get with the value_counts() and idxmax() method.

# most frequent value in Team
df['Team'].value_counts().idxmax()

Output:

'Green'

We get only one of the two modes. This happened because idxmax() returns only one value – “If multiple values equal the maximum, the first row label with that value is returned.”

Thus, it’s recommended that you use the pandas series `mode()` function to get the most frequent value in a pandas series.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *