Python | Pandas Series.value_counts()
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 toTrue
.sort
: Sorts the values by frequency by showing most common first. Set toFalse
to keep the original order.ascending
: Set toTrue
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 toFalse
if you want to include them in the result.
Example 1: Counting Unique String Values
Here we are creating a sample series.
import pandas as pd
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Chicago', 'Lisbon'])
print(sr)
Output:

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

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
import pandas as pd
sr = pd.Series([100, 214, 325, 88, None, 325, None, 325, 100])
print(sr)
Output:

print(sr.value_counts())
Output:

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.