Q&A library Python

How do I turn a categorical column into indicator columns in pandas?

Python Intermediate categoricalget-dummiesone-hot

Short answer

Use get_dummies to expand a category column into one indicator column per value. Recent pandas returns boolean columns, and drop_first avoids the redundancy that trips up some models.

The short version

Use pd.get_dummies to turn a single categorical column into several indicator columns, one per distinct value, with a True or False in each. This is one-hot encoding, the usual way to feed categories into a model that expects numbers. In pandas 2.0 and later the indicators are booleans; use dtype=int if you need 1s and 0s.

A worked example

import pandas as pd

df = pd.DataFrame({"colour": ["red", "blue", "red"]})

print(pd.get_dummies(df["colour"]))
    blue    red
0  False   True
1   True  False
2  False   True

Each original value becomes a column, and each row is True in exactly the column matching its category.

Getting numbers instead of booleans

Older pandas produced integer 1s and 0s. From pandas 2.0 the default is boolean, which many tools accept, but if you specifically need integers, ask for them.

pd.get_dummies(df["colour"], dtype=int)

If your downstream code expected 1s and 0s and suddenly sees True/False, this version change is the reason.

Encoding several columns at once

Pass the whole frame to encode every non-numeric column, with a prefix so the source column is clear.

pd.get_dummies(df)   # expands each object/category column

The new columns are named like colour_red, so you can tell which original column each came from.

The redundant-column trap

With one indicator per value, the columns are perfectly predictable from each other: if a row is not blue and not green, it must be red. For some linear models this redundancy (sometimes called the dummy variable trap) causes problems. Drop one column to remove it.

pd.get_dummies(df["colour"], drop_first=True)

This keeps all the information, because the dropped category is implied when the others are all False. Use drop_first for models sensitive to collinearity, and leave it off when you want an explicit column for every category (for example for a readable summary).

Handling unseen categories later

get_dummies only creates columns for the values it sees. If you encode training data and later apply the same step to new data with a different set of values, the columns will not line up. For a repeatable pipeline that remembers the categories, a dedicated encoder (such as scikit-learn's OneHotEncoder) is more robust than get_dummies, which is best for one-off, in-place encoding.

Need this solved on your own data?

The library is free and always will be. When the problem is bigger than an answer, we take on the work.

Start a project