value_counts() (Pandas)

s.value_counts() counts how many times each distinct value appears in a Series and returns the tallies sorted from most to least frequent. It is the fastest way to summarize a categorical column.

Pandas

value_counts() answers 'what are the categories and how common is each?' in a single call, which makes it indispensable for exploring text and category columns. The result is itself a Series, indexed by the unique values and ordered by descending count.

Two arguments unlock most of its power: normalize=True turns the counts into proportions (handy for percentages), and dropna=False includes a tally of the missing values, which the default hides. bins lets you discretize a numeric column into ranges on the fly.

For a DataFrame, value_counts() can count distinct combinations of several columns at once, effectively a quick groupby-size over those columns.

Syntax

s.value_counts(normalize=False, sort=True, dropna=True)

Examples

Python

import pandas as pd
s = pd.Series(["a", "b", "a", "a", "b"])
print(s.value_counts())

Output:

a    3
b    2
Name: count, dtype: int64

Learn it in a lesson

See also

Back to the Dictionary