
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Most Frequent Value in a Pandas Series
In this tutorial, we will learn how to display the most frequent value in a Pandas series with the help of Python. We will be using the Pandas library in this tutorial.
A series is a data structure in pandas that is similar to a column in an Excel sheet or a SQL table. It is a one-dimensional labeled data structure that can hold different data types such as integer, float, string, etc.
The most frequent value is the one that appears most often in the series. In mathematical terms, it is the mode of the data.
Approach 1
One way to display the most frequent value in a pandas series is to use the value_counts() method. It returns a series with the counts of each unique value sorted in descending order. It contains the original values in the series as its index.
Syntax
To display an image using the value_counts() method, you need to follow the following syntax ?
counts = s.value_counts() print(counts.index[0])
We use the ?value_counts()' method on a series 's' to find the most frequent value. The 'counts.index[0]' will return the index of the first value in counts. Then we will print it using the print() function.
Example
In this example, we use the Pandas library's Series() function to create a Pandas series. We are passing a list of random integers to the Series() function which returns a series, which we stored in the 's' variable. Then we will use 'counts.index[0]' to get the most frequent value in the series.
Then we will use the print() function to display the most frequent value.
import pandas as pd # create a Series with some repeated values s = pd.Series([1, 2, 2, 3, 3, 3, 4]) # use value_counts() to get the counts of each unique value counts = s.value_counts() # print the most frequent value print(counts.index[0])
Output
3
Example
In this example, we have a list of person names called 'names'. We first convert the list 'names' into a Pandas series data structure using the pd.Series() function. This series is called 'word_series'. We want to find out the most frequent name from this series.
The value_counts() method of the 'word_series' series to get the count of each unique name in the list. We store its return value in the 'word_counts' variable.
Finally, we print the most frequent name by accessing the first element of the index of the 'word_counts' Series using the print() function.
import pandas as pd # a list of words names = ['Jessica Rodriguez', 'Emily Davis', 'Michael Chen', 'Samantha Lee', 'Michael Chen', 'David Brown', 'William Wilson', 'Emily Davis', 'Sarah Kim', 'Jessica Rodriguez', 'Michael Chen', 'Samantha Lee', 'Sarah Kim', 'John Smith', 'Jessica Rodriguez', 'Jessica Rodriguez'] # create a Series from the list of words word_series = pd.Series(names) # use value_counts() to get the counts of each unique word word_counts = word_series.value_counts() # print the counts print("Most frequent name is", word_counts.index[0])
Output
Most frequent name is Jessica Rodriguez
Approach 2
Another way to display the most frequent value in a pandas series is to use the mode() method. The difference between the value_counts() method and the mode() method is that the mode() only returns the most frequent value or values if there are ties rather than the entire count of each unique value.
Syntax
To display the most frequent value using the mode() method, you need to follow the following syntax ?
mode = s.mode()[0] print(mode)
We use the 'mode()' method on a series 's' in which we want to find the most frequent value. The zeroth element in its return value will be the mode of 's'. Then we will print it using the print() function.
Example
In this example, we are using the Pandas library's Series() function to create a Pandas series. We are passing a list of random integers with some repetitions to the Series() function which creates a series data structure out of it, which we store in the 's' variable. Then we will use 's.mode()[0]' to get the most frequent value in the series.
Finally, we will use the print() function to display the mode or the most frequent value.
import pandas as pd # create a Series with some repeated values s = pd.Series([1, 2, 2, 3, 3, 3, 4]) # use value_counts() to get the counts of each unique value mode = s.mode()[0] # print the most frequent value print("The mode of the given series is", mode)
Output
The mode of the given series is 3
Example
In this example, we are using sample data of birth years of people having some repetitions. We will pass this data as a list to Pandas Series() function and store the returned series in a variable 's'. Then we will use the mode() method on 's' to get the most common birth year and store it in the 'mode' variable.
Finally, print() displays the most frequent value in our sample data.
import pandas as pd # sample data of birth years year_of_birth = [1990, 1992, 1993, 1993, 1994, 1995, 1995, 1995, 1996, 1997, 1997, 1998, 1999, 2000, 2000, 2001, 2002, 2002] # create a Series with some repeated values s = pd.Series(year_of_birth) # use value_counts() to get the counts of each unique value mode = s.mode()[0] # print the most frequent value print("The most common birth year is", mode)
Output
The most common birth year is 1995
Conclusion
We learned how to use different methods to display the most frequent value in a Pandas series data structure. We also learned how to use Pandas Series() function to create a series with custom data. The methods discussed above come in handy when we have to find the most reoccurring element in a dataset, which is very helpful for data analysts or those working with data.