0
0
Pandasdata~5 mins

value_counts() for frequency in Pandas

Choose your learning style9 modes available
Introduction

We use value_counts() to quickly see how often each unique value appears in a list or column. It helps us understand the data better.

Counting how many times each product was sold in a store.
Finding the most common colors in a list of car colors.
Checking how many people chose each option in a survey.
Seeing the frequency of different categories in a dataset.
Syntax
Pandas
Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)

value_counts() is called on a pandas Series (a single column).

The normalize=True option shows proportions instead of counts.

Examples
This counts how many times each color appears.
Pandas
import pandas as pd

colors = pd.Series(['red', 'blue', 'red', 'green', 'blue', 'blue'])
counts = colors.value_counts()
print(counts)
This shows the fraction of each color instead of counts.
Pandas
counts_normalized = colors.value_counts(normalize=True)
print(counts_normalized)
This shows counts sorted from smallest to largest.
Pandas
counts_ascending = colors.value_counts(ascending=True)
print(counts_ascending)
Sample Program

This program counts the frequency of each fruit in the list.

Pandas
import pandas as pd

# Create a Series of fruits
fruits = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'banana', 'apple'])

# Count how many times each fruit appears
fruit_counts = fruits.value_counts()

print(fruit_counts)
OutputSuccess
Important Notes

If your data has missing values (NaN), value_counts() ignores them by default.

You can use dropna=False to include missing values in the counts.

It works only on one column or Series at a time.

Summary

value_counts() helps count how often each unique value appears.

It is useful for quick data exploration and understanding categories.

You can customize sorting and show proportions instead of counts.