← Back to Pandas Course | Chapter 9: Reshaping Data | Lesson 6 of 6

explode()

explode turns each item of a list-like cell into its own row.

In this page:

  1. explode()
Syntax
python
df = df.explode("list_column")

explode()

When a cell holds a list, explode creates one row per element and repeats the other columns. The original index is repeated unless you call reset_index. It is the standard way to normalize tags or multi-valued fields.

Note: Pair explode with str.split to expand comma-separated text.

Example: explode()

python
import pandas as pd

df = pd.DataFrame({"post": ["p1", "p2"], "tags": [["py", "data"], ["sql"]]})
print(df.explode("tags").reset_index(drop=True))
s = pd.Series(["a,b", "c"]).str.split(",")
print(s.explode())

# Output:
#   post  tags
# 0   p1    py
# 1   p1  data
# 2   p2   sql
# 0    a
# 0    b
# 1    c
# dtype: object
Related Topics
Common Mistakes
  1. Forgetting the index repeats
  2. Exploding non-list values
  3. Not planning for empty lists becoming NaN
Chapter Summary
  • explode makes one row per list item
  • Other columns are repeated
  • Index repeats until reset
  • Empty lists become NaN
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.