🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.value_counts()

AI & DATA SCIENCE // df-value-counts

df.value_counts() (typically called on a Series) counts how many times each unique value appears, returning the counts sorted from most to least frequent by default.

Syntax

series.value_counts(normalize=False, sort=True)

Deep Dive Course

value_counts() is the standard, one-call way to build a frequency table for a categorical column — it's essentially grouping the column by its own unique values and taking each group's size, combined with sorting, packaged into a single convenient method. Passing normalize=True returns proportions, each count divided by the total, instead of raw counts, which is useful for quickly seeing the relative percentage breakdown of categories rather than absolute numbers.

1Understanding df.value_counts()

value_counts() is the standard, one-call way to build a frequency table for a categorical column — it's essentially grouping the column by its own unique values and taking each group's size, combined with sorting, packaged into a single convenient method. Passing normalize=True returns proportions, each count divided by the total, instead of raw counts, which is useful for quickly seeing the relative percentage breakdown of categories rather than absolute numbers.

💡

Pass normalize=True to value_counts() to get each category's proportion of the total directly, instead of computing raw counts and then manually dividing each by the total yourself.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts())
localhost:3000

2Practical Example

Here is a real-world application of df.value_counts() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts(normalize=True))
localhost:3000

3Best Practices

Follow these guidelines when working with df.value_counts():

1. Use value_counts() for a quick frequency breakdown of a categorical column, instead of a manual groupby-and-size or a Counter from the collections module

2. Pass normalize=True when proportions/percentages are more useful for your analysis than raw counts

3. Chain value_counts() with .head(n) to see just the most common categories when there are too many unique values to display usefully all at once

⚠️

Tip: Pass normalize=True to value_counts() to get each category's proportion of the total directly, instead of computing raw counts and then manually dividing each by the total yourself.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts(normalize=True))

Best Practices

  • Use value_counts() for a quick frequency breakdown of a categorical column, instead of a manual groupby-and-size or a Counter from the collections module
  • Pass normalize=True when proportions/percentages are more useful for your analysis than raw counts
  • Chain value_counts() with .head(n) to see just the most common categories when there are too many unique values to display usefully all at once

Interview Question

How does value_counts() relate to a groupby() call, given both can count occurrences of categorical values?

Hint: Think about what a groupby-and-count would look like written out manually, versus the single value_counts() call.

value_counts() is essentially a convenient, purpose-built shorthand for grouping a Series by its own unique values and counting how many times each occurs, equivalent to grouping the column by itself and taking the size of each group, then sorting the result from most to least frequent by default. It packages that specific, extremely common pattern, a frequency table of a single column, into one direct method call, rather than requiring you to write out the more general groupby()-based version yourself every time you need it.

Exercises

MediumPractice using df.value_counts() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"status": ["active", "active", "pending", "active", "cancelled"]})
print(df["status"].value_counts())

Frequently Asked Questions

How does value_counts() relate to a groupby() call, given both can count occurrences of categorical values?

value_counts() is essentially a convenient, purpose-built shorthand for grouping a Series by its own unique values and counting how many times each occurs, equivalent to grouping the column by itself and taking the size of each group, then sorting the result from most to least frequent by default. It packages that specific, extremely common pattern, a frequency table of a single column, into one direct method call, rather than requiring you to write out the more general groupby()-based version yourself every time you need it.

Related Functions

df-groupbydf-countnp-unique