Open In App

Python | Pandas Series.value_counts()

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Pandas is one of the most widely used library for data handling and analysis. It simplifies many data manipulation tasks especially when working with tabular data. In this article, we’ll explore the Series.value_counts() function in Pandas which helps you quickly count the frequency of unique values in a Series.

The value_counts() method returns a Series containing the count of unique values in the original Series. By default it sorts the result in descending order and show the most frequent values first. The syntax of function is:

Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)

Parameters:

  • normalize: Show the results as percentages instead of counts by setting this to True.
  • sort: Sorts the values by frequency by showing most common first. Set to False to keep the original order.
  • ascending: Set to True if you want to see the least frequent values first instead of the most frequent.
  • bins: For numeric data this groups the values into ranges like 0–10, 10–20. Useful for continuous numbers.
  • dropna: Skips missing (NaN) values by default. Set to False if you want to include them in the result.

Example 1: Counting Unique String Values

Here we are creating a sample series.

Python
import pandas as pd
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Chicago', 'Lisbon'])
print(sr)

Output:

Screenshot-2025-04-15-134122

Data Series

Now we will use Series.value_counts() function to find the values counts of each unique value in the given Series object.

Python
print(sr.value_counts())

Output:

Screenshot-2025-04-15-134230

Unique String Values

As we can see in the output the Series.value_counts() function returned the value counts for each unique value in the given Series object..

Example 2: Counting Numeric Values with NaN

Python
import pandas as pd

sr = pd.Series([100, 214, 325, 88, None, 325, None, 325, 100])

print(sr)

Output:

Python
print(sr.value_counts())

Output:

Screenshot-2025-04-15-134438

Numeric values with Nan

As we can see in the output Series.value_counts() function has returned the value counts of each unique value in the given Series object.


Next Article

Similar Reads