GroupBy
GroupBy Count
Tally how many rows fall into each category by looping over a category list
and incrementing a per-key counter. With pandas, df.groupby('cat').size()
performs the same split-and-count in one call and returns a Series indexed by
the category labels.
By hand
With pandas
df.groupby('cat').size() groups the rows by category and returns the row
count per group as a Series. int() converts numpy scalars for a stable
result dict.
naive.py
cats = ['a', 'b', 'a', 'c', 'b', 'a']
counts = {}
for cat in cats:
counts[cat] = counts.get(cat, 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})
counts = df.groupby('cat').size()
result = {k: int(counts[k]) for k in sorted(counts.index)}
print('index:', sorted(counts.index.tolist()))
print('values:', [int(counts[k]) for k in sorted(counts.index)])
print('dtype:', counts.dtype)
print('RESULT:', result)
index: ['a', 'b', 'c']
values: [3, 2, 1]
dtype: int64
RESULT: {'a': 3, 'b': 2, 'c': 1}
Implementation notes
groupby('cat').size()counts all rows per group, including rows with NaN in other columns. Use.count()instead to count non-null values in a specific column:df.groupby('cat')['val'].count().- The result Series is indexed by the group keys in sorted order (pandas sorts group keys by default). This matches the sorted-key dict produced in the naive half.
- Cross-reference:
group-count(python-data-basics) for the pure-Python counter version.