0
0
Data Analysis Pythondata~5 mins

value_counts() for distributions in Data Analysis Python

Choose your learning style9 modes available
Introduction

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

Checking how many times each product was sold in a store.
Finding the count of different answers in a survey question.
Seeing the number of times each category appears in a dataset.
Understanding the frequency of colors chosen in a design.
Counting how many students got each grade in a test.
Syntax
Data Analysis Python
Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)

normalize=True shows proportions instead of counts.

sort=True sorts counts from highest to lowest by default.

Examples
Counts each unique value in the column and sorts from most to least.
Data Analysis Python
df['column'].value_counts()
Shows the percentage of each unique value instead of counts.
Data Analysis Python
df['column'].value_counts(normalize=True)
Sorts counts from least to most.
Data Analysis Python
df['column'].value_counts(ascending=True)
Groups numeric data into 3 equal bins and counts values in each bin.
Data Analysis Python
df['column'].value_counts(bins=3)
Sample Program

This code counts how many times each color appears in the list and then shows the percentage for each color.

Data Analysis Python
import pandas as pd

# Sample data: colors chosen by 10 people
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue', 'red', 'yellow', 'green', 'blue']

# Create a pandas Series
color_series = pd.Series(colors)

# Count how many times each color appears
counts = color_series.value_counts()

# Show the counts
print(counts)

# Show the percentage of each color
percentages = color_series.value_counts(normalize=True)
print(percentages)
OutputSuccess
Important Notes

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

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

Summary

value_counts() helps you see how data is spread by counting each unique value.

You can get counts or percentages by changing the normalize option.

It works well for both numbers and categories.