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
Iterate cats, accumulating a running tally: counts[c] = counts.get(c, 0) + 1
inserts with zero if unseen, then increments. The final result dict sorts by
key for a deterministic comparison.
naive.py
Replay: real traced execution (multi-file project)
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)
cats ← ['A', 'B', 'A', 'C', 'B', 'A']
1cats = ['A', 'B', 'A', 'C', 'B', 'A']2counts = {}values this step['A', 'B', 'A', 'C', 'B', 'A']catscounts ← {}
1cats = ['A', 'B', 'A', 'C', 'B', 'A']2counts = {}3for c in cats:values this step{}countsc ← 'A'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'A'ccounts ← {'A': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{} → {'A': 1}countsc ← 'B'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'A' → 'B'ccounts ← {'A': 1, 'B': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{'A': 1} → {'A': 1, 'B': 1}countsc ← 'A'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'B' → 'A'ccounts ← {'A': 2, 'B': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{'A': 1, 'B': 1} → {'A': 2, 'B': 1}countsc ← 'C'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'A' → 'C'ccounts ← {'A': 2, 'B': 1, 'C': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{'A': 2, 'B': 1} → {'A': 2, 'B': 1, 'C': 1}countsc ← 'B'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'C' → 'B'ccounts ← {'A': 2, 'B': 2, 'C': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{'A': 2, 'B': 1, 'C': 1} → {'A': 2, 'B': 2, 'C': 1}countsc ← 'A'
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1values this step'B' → 'A'ccounts ← {'A': 3, 'B': 2, 'C': 1}
3for c in cats:4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}values this step{'A': 2, 'B': 2, 'C': 1} → {'A': 3, 'B': 2, 'C': 1}countsfor c in cats:
2counts = {}3for c in cats:4 counts[c] = counts.get(c, 0) + 1result ← {'A': 3, 'B': 2, 'C': 1}
4 counts[c] = counts.get(c, 0) + 15result = {k: counts[k] for k in sorted(counts)}6print('RESULT:', result)values this step{'A': 3, 'B': 2, 'C': 1}resultstdout ← RESULT: {'A': 3, 'B': 2, 'C': 1}
5result = {k: counts[k] for k in sorted(counts)}6print('RESULT:', result)values this stepRESULT: {'A': 3, 'B': 2, 'C': 1}stdout
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.
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.