Pandas: Counting the frequency of a value in a DataFrame column

Updated: February 20, 2024 By: Guest Contributor Post a comment


Pandas, a fast, powerful, flexible, and easy-to-use open-source data analysis and manipulation tool built on top of the Python programming language, offers numerous functionalities for data preparation, cleaning, and analysis. One frequent operation while working with datasets is counting the frequency of a particular value or a set of values in a DataFrame column. This tutorial will guide you through various ways to accomplish this task, ranging from simple methods to more advanced techniques.


Before we dive into the examples, ensure that you have the Pandas library installed in your Python environment. If not, you can install it using pip:

pip install pandas

Basic Example

Starting with the most basic example, let’s suppose we have a DataFrame with a column that contains several repeated values. Our objective is to count how many times each value appears in this column.

import pandas as pd

# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Alice']}
df = pd.DataFrame(data)

# Count frequency of each name
counts = df['Name'].value_counts()


Alice      2
Bob        1
Charlie    1
David      1
Name: Name, dtype: int64

This method, value_counts(), provides a quick and straightforward way to get the frequency counts directly. It automatically sorts the result in descending order by frequency.

Counting Unique Values

Another common task is counting the number of unique values in a column. This can be achieved through the nunique() method.

import pandas as pd

# Sample DataFrame
unique_data = {'Brand': ['Apple', 'Samsung', 'Apple', 'Xiaomi', 'Samsung']}
df_unique = pd.DataFrame(unique_data)

# Count number of unique brands
count_unique = df_unique['Brand'].nunique()



This example highlights how to quickly assess the diversity of entries in a specific column.

Using groupby to Count Frequencies

For more complex analyses, such as counting the frequency of values in relation to another column, Pandas groupby can be highly effective. Here’s how to utilize it:

import pandas as pd

# Sample DataFrame
sales_data = {'Product': ['Apple', 'Samsung', 'Apple', 'Xiaomi', 'Samsung'],
              'Year': [2019, 2019, 2020, 2020, 2019]}
df_sales = pd.DataFrame(sales_data)

# Group by product and count occurrences per year
df_grouped = df_sales.groupby('Product')['Year'].value_counts()


Product  Year
Apple    2019    1
         2020    1
Samsung  2019    2
Xiaomi   2020    1
Name: Year, dtype: int64

This method provides a breakdown of frequencies of values in one column across the categories of another column, enabling more refined data analysis techniques.

Counting Conditions

Sometimes, you might want to count the occurrences of values based on a specific condition. For this purpose, you can use boolean indexing combined with the sum() method.

import pandas as pd

# Sample DataFrame
condition_data = {'Product': ['Apple', 'Samsung', 'Apple', 'Xiaomi', 'Samsung'],
                  'Sales': [200, 150, 300, 100, 250]}
df_condition = pd.DataFrame(condition_data)

# Count products with sales greater than 200
count_condition = (df_condition['Sales'] > 200).sum()



This approach allows for counting occurrences that meet certain criteria, providing flexibility in data analysis tasks.

Advanced Techniques using lambda and apply

For more intricate counting operations, employing lambda functions alongside apply can offer significant versatility. Here’s an example:

import pandas as pd

# Sample DataFrame
advanced_data = {'Product': ['Apple', 'Samsung', 'Apple', 'Xiaomi', 'Samsung'],
                 'Color': ['Red', 'Blue', 'Green', 'Red', 'Green']}
df_advanced = pd.DataFrame(advanced_data)

# Count occurrences of a specific color for each product
result = df_advanced.groupby('Product')['Color'].apply(lambda x: (x == 'Red').sum())


Apple      1
Samsung    0
Xiaomi     1
Name: Color, dtype: int64

This example demonstrates the power of combining groupby, apply, and lambda functions for custom counting operations across DataFrame columns.


Counting the frequency of values within a DataFrame column is a cornerstone operation in data analysis. Starting from straightforward approaches like value_counts() and nunique(), to more complex techniques involving groupby, boolean indexing, and lambda functions, Pandas equips you with a versatile toolkit for any counting task. Mastering these methods can significantly enhance your data manipulation and analysis capabilities.