Skip to content
LearnPY
pandasfunction reference

Series.value_counts

Count how often each value appears.

Signature

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

What it does

value_counts is the quickest way to audit class balance, inspect category frequencies, and decide whether a column is too rare to encode. Use normalize=True to read proportions instead of counts.

Parameters

normalizebool, optionaloptional
Return proportions instead of counts.
sortbool, optionaloptional
Order by descending count.
dropnabool, optionaloptional
Exclude missing values.

Returns

Series

Example

pythonSeries.value_counts example
import pandas as pd

labels = pd.Series(["cat", "dog", "dog", "cat", "dog"])
print(labels.value_counts(normalize=True))

Output

dog    0.6
cat    0.4
Name: proportion, dtype: float64

Watch out for

  • The result is sorted by count, not by category order.
  • Class imbalance is easier to see with normalize=True on the training labels only.