pandas
dataframe
dummy variables
python
data analysis

How to add dummies to Pandas DataFrame?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

One-hot encoding categorical columns is a routine preprocessing step in pandas and machine learning workflows. The simplest tool is pd.get_dummies, which converts category values into 0/1 indicator columns. While the API is straightforward, production usage requires decisions around missing values, train/test alignment, high-cardinality features, and whether to drop one level to avoid perfect multicollinearity. This guide covers practical patterns for adding dummies correctly and reproducibly, including a pandas-only flow and a model-pipeline flow with scikit-learn.

Basic get_dummies Usage

For quick transformations, pass the relevant categorical columns:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "city": ["Toronto", "Paris", "Toronto", "Berlin"],
5    "plan": ["free", "pro", "pro", "free"],
6    "age": [21, 34, 29, 41],
7})
8
9encoded = pd.get_dummies(df, columns=["city", "plan"], dtype="int64")
10print(encoded)

This keeps non-categorical columns (age) unchanged and adds dummy columns such as city_Toronto and plan_pro.

If you need k-1 encoding for linear models:

python
encoded = pd.get_dummies(df, columns=["city", "plan"], drop_first=True)

Handle Missing Values and Category Drift

Missing categories can silently create inconsistent columns across datasets. Use explicit handling before encoding.

python
df["city"] = df["city"].fillna("UNKNOWN")
encoded = pd.get_dummies(df, columns=["city"], dummy_na=False)

For train/test consistency, derive columns from training data and reindex test data:

python
1train_enc = pd.get_dummies(train_df, columns=["city", "plan"])
2test_enc = pd.get_dummies(test_df, columns=["city", "plan"])
3
4test_enc = test_enc.reindex(columns=train_enc.columns, fill_value=0)

This prevents model failures from unexpected test categories or missing dummy columns.

Pipeline-Safe Encoding with scikit-learn

For production ML, prefer a pipeline so fitting and transform logic stay coupled.

python
1from sklearn.compose import ColumnTransformer
2from sklearn.preprocessing import OneHotEncoder
3from sklearn.pipeline import Pipeline
4from sklearn.linear_model import LogisticRegression
5
6cat_cols = ["city", "plan"]
7num_cols = ["age"]
8
9preprocess = ColumnTransformer(
10    transformers=[
11        ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
12        ("num", "passthrough", num_cols),
13    ]
14)
15
16model = Pipeline([
17    ("prep", preprocess),
18    ("clf", LogisticRegression(max_iter=1000)),
19])

handle_unknown="ignore" protects inference when new categories appear.

Keep Feature Names Understandable

Dummy columns can proliferate quickly. Clean naming helps debugging and model interpretation.

python
1encoded = pd.get_dummies(
2    df,
3    columns=["city"],
4    prefix={"city": "city"},
5    prefix_sep="__",
6)

For high-cardinality features (for example, zip codes, product IDs), one-hot encoding may explode dimensionality. Consider target encoding, hashing, or grouping rare categories before encoding.

Practical Verification Workflow

A reliable way to avoid regressions is to validate the solution in three passes: baseline, controlled change, and repeatability check. First, capture a baseline outcome before you apply fixes. This could be a failing command, a wrong output sample, a stack trace, or a screenshot of current behavior. Second, apply one focused change and rerun exactly the same checks so you can attribute improvements to a specific edit. Third, rerun the checks multiple times or with slightly different inputs to ensure the fix is not accidental or data-specific.

A lightweight template you can adapt for most projects looks like this:

bash
1# 1) reproduce current behavior
2./run_example.sh > before.txt
3
4# 2) apply your change
5# edit config/code based on this article
6
7# 3) verify behavior after change
8./run_example.sh > after.txt
9diff -u before.txt after.txt

If your environment involves tests, add at least one focused regression test that would fail before the fix and pass after it. This turns a one-time troubleshooting success into a durable maintenance improvement, which is especially important when teams rotate ownership or upgrade dependencies later.

Common Pitfalls

  • Encoding train and test independently without aligning columns before prediction.
  • Using drop_first=True blindly for tree-based models where it is usually unnecessary.
  • Forgetting to handle unknown categories, causing inference-time errors.
  • One-hot encoding very high-cardinality columns and creating huge sparse matrices.
  • Losing track of feature names, making model inspection and debugging difficult.

Summary

Use pd.get_dummies for fast dataframe transformations and a pipeline-based encoder for production ML systems. Handle missing/unknown categories explicitly, keep train/test columns aligned, and choose encoding strategy based on model type and feature cardinality. With these practices, dummy-variable generation stays reliable from notebook experiments to deployed inference.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.