Categorical Encoding
One-Hot Encode Small
Create one binary column per category. For each row, the column for its
category is 1 and all other columns are 0. By hand, build a per-category
indicator list with a nested loop. With pandas, pd.get_dummies() does the
same in one call — cast to int for 0/1 output since pandas returns bool by
default.
By hand
Build the result as a dict from category name to indicator list. An init
loop creates an empty list per category; the outer loop walks the labels and
the inner loop appends 1 when s == c and 0 otherwise. The trace shows
each category's list growing in lockstep across all 4 rows.
labels = ['cat', 'dog', 'bird', 'cat']
cats = sorted(set(labels))
result = {}
for c in cats:
result[c] = []
for s in labels:
for c in cats:
result[c].append(1 if s == c else 0)
print('RESULT:', result)
labels ← ['cat', 'dog', 'bird', 'cat']
1labels = ['cat', 'dog', 'bird', 'cat']2cats = sorted(set(labels))values this step['cat', 'dog', 'bird', 'cat']labelscats ← ['bird', 'cat', 'dog']
1labels = ['cat', 'dog', 'bird', 'cat']2cats = sorted(set(labels))3result = {}values this step['bird', 'cat', 'dog']catsresult ← {}
2cats = sorted(set(labels))3result = {}4for c in cats:values this step{}resultc ← 'bird'
3result = {}4for c in cats:5 result[c] = []values this step'bird'cresult ← {'bird': []}
4for c in cats:5 result[c] = []6for s in labels:values this step{} → {'bird': []}resultc ← 'cat'
3result = {}4for c in cats:5 result[c] = []values this step'bird' → 'cat'cresult ← {'bird': [], 'cat': []}
4for c in cats:5 result[c] = []6for s in labels:values this step{'bird': []} → {'bird': [], 'cat': []}resultc ← 'dog'
3result = {}4for c in cats:5 result[c] = []values this step'cat' → 'dog'cresult ← {'bird': [], 'cat': [], 'dog': []}
4for c in cats:5 result[c] = []6for s in labels:values this step{'bird': [], 'cat': []} → {'bird': [], 'cat': [], 'dog': []}resultfor c in cats:
3result = {}4for c in cats:5 result[c] = []s ← 'cat'
5 result[c] = []6for s in labels:7 for c in cats:values this step'cat'sc ← 'bird'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'dog' → 'bird'cresult ← {'bird': [0], 'cat': [], 'dog': []}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [], 'cat': [], 'dog': []} → {'bird': [0], 'cat': [], 'dog': []}resultc ← 'cat'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'bird' → 'cat'cresult ← {'bird': [0], 'cat': [1], 'dog': []}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0], 'cat': [], 'dog': []} → {'bird': [0], 'cat': [1], 'dog': []}resultc ← 'dog'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'cat' → 'dog'cresult ← {'bird': [0], 'cat': [1], 'dog': [0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0], 'cat': [1], 'dog': []} → {'bird': [0], 'cat': [1], 'dog': [0]}resultfor c in cats:
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)s ← 'dog'
5 result[c] = []6for s in labels:7 for c in cats:values this step'cat' → 'dog'sc ← 'bird'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'dog' → 'bird'cresult ← {'bird': [0, 0], 'cat': [1], 'dog': [0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0], 'cat': [1], 'dog': [0]} → {'bird': [0, 0], 'cat': [1], 'dog': [0]}resultc ← 'cat'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'bird' → 'cat'cresult ← {'bird': [0, 0], 'cat': [1, 0], 'dog': [0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0], 'cat': [1], 'dog': [0]} → {'bird': [0, 0], 'cat': [1, 0], 'dog': [0]}resultc ← 'dog'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'cat' → 'dog'cresult ← {'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0], 'cat': [1, 0], 'dog': [0]} → {'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]}resultfor c in cats:
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)s ← 'bird'
5 result[c] = []6for s in labels:7 for c in cats:values this step'dog' → 'bird'sc ← 'bird'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'dog' → 'bird'cresult ← {'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]} → {'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]}resultc ← 'cat'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'bird' → 'cat'cresult ← {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]} → {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]}resultc ← 'dog'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'cat' → 'dog'cresult ← {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]} → {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}resultfor c in cats:
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)s ← 'cat'
5 result[c] = []6for s in labels:7 for c in cats:values this step'bird' → 'cat'sc ← 'bird'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'dog' → 'bird'cresult ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]} → {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}resultc ← 'cat'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'bird' → 'cat'cresult ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]} → {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]}resultc ← 'dog'
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)values this step'cat' → 'dog'cresult ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}
7 for c in cats:8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this step{'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]} → {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}resultfor c in cats:
6for s in labels:7 for c in cats:8 result[c].append(1 if s == c else 0)for s in labels:
5 result[c] = []6for s in labels:7 for c in cats:stdout ← RESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}
8 result[c].append(1 if s == c else 0)9print('RESULT:', result)values this stepRESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}stdout
With pandas
pd.get_dummies(df['c']) returns a DataFrame with one boolean column per
category, in sorted alphabetical order. Chain .astype(int) to convert
True/False to 1/0 — pandas 2.x returns bool by default. The
snapshot shows dtype: int64 after the cast and the per-column indicator
lists.
import pandas as pd
from dalib.display import set_display
set_display()
labels = ['cat', 'dog', 'bird', 'cat']
df = pd.DataFrame({'c': labels})
ohe = pd.get_dummies(df['c']).astype(int)
result = {c: ohe[c].tolist() for c in ohe.columns}
print('columns:', ohe.columns.tolist())
print('dtype:', ohe.dtypes[0])
print('RESULT:', result)
columns: ['bird', 'cat', 'dog']
dtype: int64
RESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}
Implementation notes
pd.get_dummies()sorts columns alphabetically — the same order assorted(set(labels))in the naive half, so RESULT columns align.- The default dtype in pandas 2.x is
bool. Add.astype(int)when downstream code expects 0/1 integers rather than True/False. - One-hot creates k columns for k categories;
label-encode(this chapter) creates a single integer column — both represent the same information but one-hot avoids implying ordinal ranking. - Cross-reference:
label-encode(this chapter) for the integer-code alternative.