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)
  1. cats ← ['A', 'B', 'A', 'C', 'B', 'A']

    1cats = ['A', 'B', 'A', 'C', 'B', 'A']2counts = {}
    values this step['A', 'B', 'A', 'C', 'B', 'A']cats
  2. counts ← {}

    1cats = ['A', 'B', 'A', 'C', 'B', 'A']2counts = {}3for c in cats:
    values this step{}counts
  3. c ← 'A'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'A'c
  4. counts ← {'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}counts
  5. c ← 'B'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'A' 'B'c
  6. counts ← {'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}counts
  7. c ← 'A'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'B' 'A'c
  8. counts ← {'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}counts
  9. c ← 'C'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'A' 'C'c
  10. counts ← {'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}counts
  11. c ← 'B'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'C' 'B'c
  12. counts ← {'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}counts
  13. c ← 'A'

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
    values this step'B' 'A'c
  14. counts ← {'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}counts
  15. for c in cats:

    2counts = {}3for c in cats:4    counts[c] = counts.get(c, 0) + 1
  16. result ← {'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}result
  17. stdout ← 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 pass sort=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 via groupby.