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.
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.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]
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
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
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
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
[90, 85, 95] for Ravi and [70, 75] for Raju, resetting the index cleanly. Verify that 5 total rows are produced.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
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.