Apply and Values
Value Counts
Count occurrences of each distinct value in a column. By hand, a running dict
accumulates the tally via .get. With pandas, Series.value_counts() returns
a sorted frequency Series in one call.
By hand
With pandas
df['cat'].value_counts() returns a Series with category values as the index
and counts as values, sorted by frequency descending. The snapshot shows the
default frequency order; result re-sorts by key to match the naive.
naive.py
cats = ['A', 'B', 'A', 'C', 'B', 'A']
counts = {}
for c in cats:
counts[c] = counts.get(c, 0) + 1
result = {k: counts[k] for k in sorted(counts)}
print('RESULT:', result)
library.py
import pandas as pd
from dalib.display import set_display
set_display()
cats = ['A', 'B', 'A', 'C', 'B', 'A']
df = pd.DataFrame({'cat': cats})
vc = df['cat'].value_counts()
result = {k: int(vc[k]) for k in sorted(vc.index)}
print('index:', vc.index.tolist())
print('values:', vc.tolist())
print('dtype:', vc.dtype)
print('RESULT:', result)
index: ['A', 'B', 'C']
values: [3, 2, 1]
dtype: int64
RESULT: {'A': 3, 'B': 2, 'C': 1}
Implementation notes
value_counts()sorts by frequency descending by default. Chain.sort_index()(or passsort=False) to get values in key order instead.- Cross-reference:
frequency-count(python-data-basics) for the pure-Python version;groupby-count(ch04) for the same operation viagroupby.