Pandasdrill

Cleaning Updated November 2026

explode

df.explode("col") creates one row for each element of the list in col, repeating the other columns and keeping the original index label on every new row. Split delimited strings into lists first with str.split, and add ignore_index=True for a clean index.

Syntax

df.explode(column, ignore_index=False)
PartWhat it is
columnColumn name (or list of names with equal-length lists) holding list-like values
ignore_indexTrue renumbers the result 0..n-1

Examples

import pandas as pd

df = pd.DataFrame({"order": [1, 2], "items": [["pen", "ink"], ["pad"]]})
df.explode("items")

Result: order items 0 1 pen 0 1 ink 1 2 pad

The index label repeats for rows that came from the same original row.

tags = pd.DataFrame({"post": ["a", "b"], "tags": ["python,pandas", "sql"]})
tags.assign(tags=tags["tags"].str.split(",")).explode("tags", ignore_index=True)

Result: post tags 0 a python 1 a pandas 2 b sql

df.explode("items")["items"].value_counts()

Result: items pen 1 ink 1 pad 1 Name: count, dtype: int64

Explode then value_counts is the standard way to count tags across rows.

Common mistakes

  • Nothing changes: the column holds strings like "[a, b]", not lists; split them (str.split) or parse them (ast.literal_eval) first.
  • Duplicate index labels after explode break later joins; pass ignore_index=True.
  • ValueError: columns must have matching element counts when exploding several columns; lists must be the same length row by row.

Practice explode

These drills check your answer on live data, free, no account needed.

  • Pull numbers out of codesEach code looks like ORD-2024-0153. Extract the final group of digits as an integer (so 0153 becomes 153) and return it as a Series.
  • Wide to long with meltTurn `wide` (one column per quarter) into a long table with the columns `store`, `quarter` and `sales`.

Questions

What is the opposite of explode?

Group and collect: df.groupby("order")["items"].agg(list) rebuilds the list column.

How do I explode a dict column into columns?

That is not explode; use pd.json_normalize(df["col"]) or df["col"].apply(pd.Series) to spread keys into columns.

Related

  • str.splitHow to split a string column in pandas with str.split: into separate columns with expand=True, limit the number of splits, take one part with str[0], regex.
  • meltHow to use melt in pandas to turn wide data (one column per month or category) into long format with a variable and a value column, keeping id columns.
  • value_countsHow to count values in a pandas column with value_counts: counts sorted by frequency, percentages with normalize=True, including NaN, and two-column combos.
  • groupbyHow groupby works in pandas: group by one or more columns, apply sum, mean, count or size, get a flat table with as_index=False, and iterate over groups.

Previous: transform