Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

pd.get_dummies

Converts categorical values into 0/1 indicator columns. DataFrame inputs can selectively encode columns and retain other columns.

How it works

pd.get_dummies converts categorical values into indicator columns containing 0 or 1. For a DataFrame, selected columns—or, when columns is omitted, columns with object, string, or category dtype—are expanded and named using the source column and category value.

Syntax

pandas.get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False, dtype=None)

Parameters

NameTypeDescription
dataarray-like, Series, or DataFrameArray-like, Series, or DataFrame containing the data to encode.
prefixstr, list of str, or dict of str, default NoneString, list of strings, or mapping used to prefix DataFrame column names; default None.
prefix_sepstr, default '_'Separator used between a prefix and category value; default '_'.
dummy_nabool, default FalseWhether to add an indicator for missing values; default False.
columnslist-like, default NoneDataFrame column names to encode; default None.
sparsebool, default FalseWhether generated columns use SparseArray backing; default False.
drop_firstbool, default FalseWhether to omit the first category level; default False.
dtypedtype, default NoneData type for the generated indicators; default None.

Returns

DataFrame of dummy-coded data.

Key points

  • The function transforms categorical data into numeric indicator variables.
  • A categorical variable receives one binary column per distinct value unless a level is dropped.
  • Generated columns use category values in their names; DataFrame inputs also include the original column name.
  • prefix customizes the text prepended to encoded DataFrame column names.
  • Use columns to select which DataFrame fields should be converted.
  • sparse=True stores the encoded columns with sparse-array backing rather than regular dense storage.

Common mistakes

  • With dummy_na=False, missing values do not receive a separate indicator and are represented by zeros.
  • Pass one dtype for all generated indicator columns; different dtypes per output column are not supported.

Example: Encode a DataFrame column

import pandas as pd

df = pd.DataFrame({"color": ["red", "blue", "red"], "value": [1, 2, 3]})
encoded = pd.get_dummies(df, columns=["color"], dtype=int)
print(encoded)

Encodes the selected categorical column as integer indicator columns while retaining the numeric column.

When to use it

Feature encoding

Prepare categorical features for a machine-learning model.

Data transformation

Turn labeled categories into analysis-ready indicator columns.

Missing-value indicators

Represent missingness explicitly as a model feature.

Interview questions

QWhat does pd.get_dummies do?

It creates one indicator column for each distinct category in the input. For DataFrame input, generated names combine the original column name with the category value.

Follow-up: How would you avoid encoding every eligible column? Pass an explicit list of column names through columns.

QWhich DataFrame columns are encoded?

By default, columns is None, so object, string, and category columns are converted. Supplying columns restricts encoding to the named DataFrame columns.

Follow-up: What happens to columns not listed in columns? They remain in the returned DataFrame unchanged.

QWhat is the effect of drop_first?

drop_first=True removes the first category level, producing k−1 indicator columns for k levels. This can remove a redundant reference category in models.

Follow-up: How are missing values handled when dummy_na=True? A separate indicator column is added for missing values.

  • pandas.from_dummies
  • pandas.Categorical
  • pandas.Series.str.get_dummies

Source: pandas.get_dummies. Checked 2026-10-15.