Open In App

Count Frequency of Columns in Pandas DataFrame

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
8 Likes
Like
Report

Counting how often each value appears in a column is a basic step in exploring your dataset. It helps you understand data distribution and identify patterns. Here are four common ways to count frequencies in a Pandas DataFrame.

1. Using value_counts()

The simplest method is value_counts(), which returns a Series with unique values as index and their counts as values.

Python
import pandas as pd
data = {
    'Fruits': ['Apple', 'Banana', 'Apple', 'Orange', 'Banana', 'Apple', 'Orange'],
    'Quantity': [3, 2, 3, 1, 2, 3, 1]
}
df = pd.DataFrame(data)
fruit_counts = df['Fruits'].value_counts()  # Counts how many times each unique fruit appears in the 'Fruits' column

print(fruit_counts)

Output

Screenshot-2025-04-14-152958
Count frequency

You can use normalize=True to get relative frequencies or ascending=True to sort counts in ascending order.

2. Using groupby() with size()

When you want to group data by a specific column and count the frequency of each group, groupby() combined with size() is used for it.

Python
fruit_counts = df.groupby('Fruits').size()
print(fruit_counts)

Output

Screenshot-2025-04-14-153451
Using groupby with size

The groupby() method can also be extended to multiple columns to count the frequency of values across combinations of categories.

3. Using crosstab()

For counting occurrences across two or more categories, crosstab() is useful. crosstab() provides a table format which is helpful for cross-dimensional analysis.

Python
fruit_counts = pd.crosstab(index=df['Fruits'], columns='count')
print(fruit_counts)

Output

Screenshot-2025-04-14-153935
Using crosstab

Explanation:

  • pd.crosstab(): Creates a cross-tabulation table that counts occurrences of values.
  • index=df['Fruits']: Uses the unique values in the Fruits column as the rows of the table.
  • columns='count': Creates a single column labeled 'count' to store the frequency of each fruit.

4. Using a Pivot Table

A pivot table is another tool in Pandas which can be used to calculate frequency counts. It works well if we're looking to summarize data across multiple dimensions.

Python
fruit_counts = df.pivot_table(index='Fruits', aggfunc='size')
print(fruit_counts)

Output

Screenshot-2025-04-14-153451
Using Pivot Table

Article Tags :

Explore