How to calculate the frequency of each item in a Pandas series?

In this program, we will calculate the frequency of each element in a Pandas series. The function value_counts() in the pandas library helps us to find the frequency of elements.

Algorithm

Step 1: Define a Pandas series.
Step 2: Print the frequency of each item using the value_counts() function.

Example Code

import pandas as pd

series = pd.Series([10, 10, 20, 30, 40, 30, 50, 10, 60, 50, 50])
print("Series:\n", series)

frequency = series.value_counts()
print("\nFrequency of elements:\n", frequency)

Output

Series:
 0     10
1     10
2     20
3     30
4     40
5     30
6     50
7     10
8     60
9     50
10    50
dtype: int64

Frequency of elements:
 50    3
10    3
30    2
20    1
40    1
60    1
dtype: int64

Sorting Options

By default, value_counts() returns frequencies in descending order. You can control the sorting behavior ?

import pandas as pd

series = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'apple'])

# Sort by frequency (ascending)
print("Ascending frequency:")
print(series.value_counts(ascending=True))

print("\nSort by index:")
print(series.value_counts(sort=False))
Ascending frequency:
orange    1
banana    2
apple     3
dtype: int64

Sort by index:
apple     3
banana    2
orange    1
dtype: int64

Including Missing Values

The dropna parameter controls whether to include missing values in the frequency count ?

import pandas as pd
import numpy as np

series = pd.Series([1, 2, 2, np.nan, 3, np.nan])

print("Excluding NaN values (default):")
print(series.value_counts())

print("\nIncluding NaN values:")
print(series.value_counts(dropna=False))
Excluding NaN values (default):
2.0    2
1.0    1
3.0    1
dtype: int64

Including NaN values:
2.0    2
NaN    2
1.0    1
3.0    1
dtype: int64

Conclusion

The value_counts() function is essential for frequency analysis in Pandas. Use ascending=True to sort frequencies in ascending order, and dropna=False to include missing values in the count.

Updated on: 2026-03-25T17:57:42+05:30

584 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements