Pandas Explode and get_dummies
last modified November 17, 2026 Python 3.15
Two common preparation steps in data analysis are expanding list-like cells
into separate rows and turning category labels into numeric columns. Pandas
handles the first with explode and the second with
get_dummies. Both change the shape of a DataFrame rather than the
values inside a single cell.
This article explains how explode repeats index labels and how
ignore_index fixes that, then introduces one-hot encoding with
get_dummies, its drop_first and prefix
options, the boolean and integer dtypes it can produce, and finally how the two
methods work together. All examples were run with pandas 3.1.1.
Exploding list-like cells
A cell can hold a list of values, for example a row of tags. The
explode method turns each element of that list into its own row
and copies the other columns of the original row alongside it. An important
detail is that the index label is repeated: a row with two tags becomes two
rows that share the same index value.
import pandas as pd
df = pd.DataFrame({'tags': [['a', 'b'], ['c'], ['a', 'c']]})
print(df.explode('tags'))
The first original row contained two tags and produced two rows labelled 0; the second had one tag and produced one row labelled 1; the third produced two rows labelled 2. The repeated labels are visible in the left-hand index column and can matter for later operations that rely on the index.
$ python explode_basic.py tags 0 a 0 b 1 c 2 a 2 c
ignore_index
When repeated index labels are not wanted, ignore_index=True
discards the old labels and numbers the resulting rows from zero. This is
useful after exploding because the new rows are distinct records even when they
came from the same original cell.
import pandas as pd
df = pd.DataFrame({'tags': [['a', 'b'], ['c'], ['a', 'c']]})
print(df.explode('tags', ignore_index=True))
The same five values appear, but the index now runs from 0 to 4 with no repeats. The original grouping information is no longer in the index; it would have to be kept in a separate column if it were still needed.
$ python explode_ignore.py tags 0 a 1 b 2 c 3 a 4 c
One-hot encoding
One-hot encoding replaces one column of category labels with several
new columns, one for each possible category. In each row a single new column is
marked as present and all the others are marked as absent, which lets a model
that only understands numbers work with categorical data. The
pd.get_dummies function does this and prefixes each new column
with the original column name.
import pandas as pd
df = pd.DataFrame({'color': ['red', 'blue', 'red', 'green']})
print(pd.get_dummies(df))
The single color column becomes three columns:
color_blue, color_green and color_red.
Each row has exactly one True among them, in the column matching
its original colour.
$ python dummies_basic.py color_blue color_green color_red 0 False False True 1 True False False 2 False False True 3 False True False
drop_first
Because one column is always enough to identify the category, keeping every
dummy column can be redundant. Setting drop_first=True removes the
first category, leaving n minus one columns for n categories.
The dropped category is represented by all remaining columns being false.
import pandas as pd
df = pd.DataFrame({'color': ['red', 'blue', 'red', 'green']})
print(pd.get_dummies(df, drop_first=True))
The alphabetically first category, blue, is gone. A row originally labelled
blue is now indicated by both color_green and
color_red being false. This avoids perfect duplication among the
columns, which some statistical models require.
$ python dummies_drop_first.py color_green color_red 0 False True 1 False False 2 False True 3 True False
prefix
When several columns are encoded at once, short names help keep the new columns
readable. The prefix parameter takes a list of strings, one per
column being encoded, and uses them instead of the original column names. The
number of prefixes must match the number of encoded columns.
import pandas as pd
df = pd.DataFrame({'color': ['red', 'blue', 'red'], 'size': ['S', 'L', 'M']})
print(pd.get_dummies(df, prefix=['col', 'sz']))
The colour columns now start with col_ and the size columns with
sz_. Each original category still gets its own column, so we end
up with three colour columns and three size columns in total.
$ python dummies_prefix.py col_blue col_red sz_L sz_M sz_S 0 False True False False True 1 True False True False False 2 False True False True False
Resulting dtypes
On pandas 3.1.1 the dummy columns are boolean, holding True and
False. Passing dtype=int produces integer columns of
1 and 0 instead, which some tools prefer. The two forms carry the same
information; only the stored type changes.
import pandas as pd
df = pd.DataFrame({'color': ['red', 'blue', 'red']})
print(pd.get_dummies(df))
print()
print(pd.get_dummies(df, dtype=int))
The first table uses True and False, the second uses
1 and 0. Both mark red in rows 0 and 2 and blue in row 1. See the
Pandas data types tutorial for more about
dtypes and how they affect memory use and calculations.
$ python dummies_dtype.py color_blue color_red 0 False True 1 True False 2 False True color_blue color_red 0 0 1 1 1 0 2 0 1
Combining explode and get_dummies
The two methods fit together naturally. We start with rows that carry a list
of tags, explode the list so each tag has its own row, and then one-hot encode
the tag column. Numeric columns such as order are left untouched
by get_dummies when we name only tags in its
columns argument.
import pandas as pd
df = pd.DataFrame({
'order': [1, 2],
'tags': [['red', 'blue'], ['green', 'red']]
})
exploded = df.explode('tags', ignore_index=True)
print(exploded)
print()
print(pd.get_dummies(exploded, columns=['tags']))
After exploding there are four rows, two per order. The final table keeps the
order numbers and adds the three tag columns
tags_blue, tags_green and tags_red. Each
row has one true tag, and the shared order value shows which tags
belonged together originally.
$ python explode_dummies_combine.py order tags 0 1 red 1 1 blue 2 2 green 3 2 red order tags_blue tags_green tags_red 0 1 False False True 1 1 True False False 2 2 False True False 3 2 False False True
Source
Pandas DataFrame.explode Documentation, Pandas get_dummies Documentation, and Pandas Reshaping Documentation
In this article we have expanded list-like cells with explode,
reset their index with ignore_index, built one-hot encoded columns
with get_dummies, reduced them with drop_first, named
them with prefix, compared boolean and integer dtypes, and used
both methods on one dataset. For other ways to reshape a table see
Pandas melt and pivot and
Pandas pivot table.
Author
List all Pandas tutorials.