Python

Pandas Get Most Frequent Value

The goal of this article is to show the most frequent value in a set of numbers. To sum the number of times an element or number appears, Python’s value_counts() function is used. The mode() method can then be used to get the most often occurring element. If you want different ways to get the most frequent values in Python, this article has all the guidelines.

What Is the Value_counts() Method in Python?

A Pandas object’s unique values are counted using the value counts() method. In Python, we generally employ this technique for data wrangling as well as data exploration.

The value_counts() method can work with a variety of Pandas objects. Pandas series, Pandas dataframes, and dataframe columns are examples of these (which are Pandas Series objects).

However, depending on the sort of object you’re working with, how you implement the value_counts() method will differ slightly.

Other optional arguments can be used to alter the functionality of the value_counts() method.

Syntax of Pandas Series Mode() Function

In a pandas series, the most common value is simply the series’ mode. The pandas series mode() method is used to acquire information about the mode. The syntax is as follows. The modes of the series are returned in sorted order.

# df['Column'].mode()

Syntax of Pandas Value_counts() Function

To fetch the highest count value, use the pandas value_counts() and idxmax() functions simultaneously. The syntax is as follows:

# df['Column'].value_counts().idxmax()

Now let’s look at some practical examples to see how you may achieve the most frequent values by following which steps.

Example1:

We must first establish the dataframe before proceeding to the steps of determining the most frequent value with mode(). This is a dataframe with a category field that we’ll use for the rest of the tutorial. The dataframe ‘d_frame’ contains the names (‘Kim’, ‘Kourtney’, ‘Scott’, ‘Rob’, ‘Kendall’, ‘Gathie’, ‘Phill’) and team information (‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘A’, ‘B’, ‘A’, ‘B’, ‘A’). The dataframe’s “Team” column is a category field with values denoting the team assigned to each student.

The pandas module is imported at the beginning of the code in the reference code below. The dataframe is then generated and presented on the screen.

import pandas
d_frame = pandas.DataFrame({
    'Name': ['Kim', 'Kourtney', 'Scott', 'Rob', 'Kendall', 'Gathie', 'Phill'],
    'Team': ['A', 'B', 'C', 'D', 'E', 'A', 'B']
})
print(d_frame)

In the image below, the students’ names are displayed together with the team’s name to which they have been assigned.

We will show you how to use the mode() function to determine the most frequent value. The mode, which is a descriptive statistic, is basically the most common value in the dataset. It will give you information about the team that has the most students.

We have imported the pandas module first and generated the dataframe, as you can see in the code. The names of the students and the team are included in the dataframe.

import pandas
d_frame = pandas.DataFrame({
    'Name': ['Kim', 'Kourtney', 'Scott', 'Rob', 'Kendall', 'Gathie', 'Phill'],
    'Team': ['A', 'B', 'C', 'D', 'E', 'A', 'B']
})
print(d_frame['Team'].mode())

It gives a pandas series plus the mode of the column. Because “A” and “B” are the most frequent values in the “Team” field, we obtain “A” and “B” as the mode.

Please note that you can acquire the mode of each column in a pandas dataframe by using the mode() method.

Example 2:

We will show you how to use value_counts() to get the most frequent value in this example. value_counts() function can be used to obtain counts, and then the idxmax() function can be used to obtain the value with the most counts.

The rest of the code, except for the last line, is identical to the one above. It demonstrates how the function (value_counts) is used to find out the value with the highest count.

import pandas
d_frame = pandas.DataFrame({
    'Name': ['Kim', 'Kourtney', 'Scott', 'Rob', 'Kendall', 'Gathie', 'Phill'],
    'Team': ['A', 'B', 'C', 'D', 'E', 'A', 'A']
})
print(d_frame['Team'].value_counts().idxmax())

See the resultant screen below. We get the value in the “Team” column with the maximum value count.

Example 3:

This example will demonstrate what will happen if the dataframe contains the most frequently occurring values. Let’s change the dataframe so that the “Team” column contains repeated modes. We change “Rob’s” “Team” value from “D” to “B” here.

import pandas
d_frame = pandas.DataFrame({
    'Name': ['Kim', 'Kourtney', 'Scott', 'Rob', 'Kendall', 'Gathie', 'Phill'],
    'Team': ['A', 'B', 'C', 'D', 'E', 'A', 'F']
})
d_frame.at[3, 'Team'] = 'B'
print(d_frame)

We now have recurring modes, as you can see. “A” appears twice in the “Team” column in our scenario.

The team name for the student ‘Rob’ has been changed from “D” to “A” in the accompanying image.

Example 4:

Let’s see what the value counts() and idxmax() methods return. We have updated the dataframe values in this example code. Notice that the team “A” and “B” appear two times. After that, we used the value.counts() and idxmax() functions to determine the most common value in the dataframe. Here is the reference code.

import pandas
d_frame = pandas.DataFrame({
    'Name': ['Kim', 'Kourtney', 'Scott', 'Rob', 'Kendall', 'Gathie', 'Phill'],
    'Team': ['A', 'B', 'C', 'D', 'E', 'A', 'B']
})
print(d_frame['Team'].value_counts().idxmax())

Please notice that even if there are many modes present, this method only returns a single value. This happened because the idxmax() function delivers only one result – “If multiple values match the maximum, the one-row title with that value is returned.” To retrieve the most common value in a pandas series, you need to apply the pandas series’s ‘mode()’ function.

Conclusion:

In this article, we looked at how to find the most frequent value in a pandas column or series using certain examples. We’ve discussed a variety of functions that can be used to accomplish this goal. Mode(), value counts(), and idxmax() are some of these methods. If you are new to this concept and need a step-by-step guide to getting started, go no further than this article.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content