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

## How to get the most frequent value in a pandas series?

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.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

### 1. Most frequent value with `mode()`

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.

### 2. Most frequent value with `value_counts()`

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.

## What happens if we have two values that are most frequent?

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.**