Pandas: Explode List-Like Columns into Rows

Transform columns containing lists, tuples, or arrays into individual rows using the Pandas explode() method. Flatten nested data structures, manage row indexes, handle missing values, and unpack multi-column lists cleanly.

Introduction to explode() 🔝

The explode() method converts each element of a list-like structure (such as lists, tuples, sets, or Series) into a new row while replicating index values and other non-exploded columns. It is essential when parsing nested JSON or API responses into clean relational tables.

Create nested list sample data 🔝

Let us start with a student dataset where subjects are stored as lists within a single column.
import pandas as pd
df = pd.DataFrame({
    "NAME": ["Ravi", "Raju", "Alex"],
    "ID": [1, 2, 3],
    "SUBJECTS": [["Math", "Physics"], ["English"], ["Chemistry", "Biology", "Math"]]
})
print(df)
assert df.shape == (3, 3)

Expected output

   NAME  ID                       SUBJECTS
0  Ravi   1                [Math, Physics]
1  Raju   2                      [English]
2  Alex   3  [Chemistry, Biology, Math]

Basic explode on a single column 🔝

Call df.explode("SUBJECTS") to convert list elements into separate rows while retaining original row index labels.
exploded = df.explode("SUBJECTS")
print(exploded)
assert len(exploded) == 6

Expected output

   NAME  ID   SUBJECTS
0  Ravi   1       Math
0  Ravi   1    Physics
1  Raju   2    English
2  Alex   3  Chemistry
2  Alex   3    Biology
2  Alex   3       Math

Resetting index with ignore_index 🔝

Pass ignore_index=True to reset row index labels and generate a fresh continuous RangeIndex for the resulting DataFrame.
reset = df.explode("SUBJECTS", ignore_index=True)
print(reset)
assert reset.index.tolist() == list(range(6))

Expected output

   NAME  ID   SUBJECTS
0  Ravi   1       Math
1  Ravi   1    Physics
2  Raju   2    English
3  Alex   3  Chemistry
4  Alex   3    Biology
5  Alex   3       Math

Exploding multiple matching columns 🔝

In modern Pandas, you can pass a list of column names to explode() provided the list columns in each row have matching lengths.
multi_df = pd.DataFrame({
    "NAME": ["Ravi", "Raju"],
    "SUBJECTS": [["Math", "Physics"], ["English", "History"]],
    "MARKS": [[85, 90], [78, 88]]
})
multi_exploded = multi_df.explode(["SUBJECTS", "MARKS"], ignore_index=True)
print(multi_exploded)
assert len(multi_exploded) == 4

Expected output

   NAME SUBJECTS  MARKS
0  Ravi     Math     85
1  Ravi  Physics     90
2  Raju  English     78
3  Raju  History     88

Handling empty lists and missing values 🔝

When explode() encounters empty lists [] or missing values NaN, it preserves the row and places NaN in the exploded column.
empty_df = pd.DataFrame({
    "NAME": ["Ravi", "Raju"],
    "SUBJECTS": [["Math"], []]
})
empty_exploded = empty_df.explode("SUBJECTS", ignore_index=True)
print(empty_exploded)
assert pd.isna(empty_exploded.iloc[1]["SUBJECTS"])

Expected output

   NAME SUBJECTS
0  Ravi     Math
1  Raju      NaN

Exercise: explode student subjects 🔝

Explode a DataFrame containing student names and nested lists of grade scores [90, 85, 95] for Ravi and [70, 75] for Raju, resetting the index cleanly. Verify that 5 total rows are produced.

Exercise solution 🔝

Solution creating the DataFrame and applying explode().
ex_df = pd.DataFrame({
    "NAME": ["Ravi", "Raju"],
    "SCORES": [[90, 85, 95], [70, 75]]
})
answer = ex_df.explode("SCORES", ignore_index=True)
print(answer)
assert len(answer) == 5 and answer.index.tolist() == list(range(5))

Expected output

   NAME SCORES
0  Ravi     90
1  Ravi     85
2  Ravi     95
3  Raju     70
4  Raju     75

Practice in Google Colab 🔝

Open in Google Colab View on GitHub

Run examples in order, inspect exploded lists and try the exercise. Save a copy to keep edits.

Reference: Pandas explode() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer