🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.explode()

AI & DATA SCIENCE // df-explode

df.explode() transforms each element of a list-like column into its own separate row, duplicating the other columns' values across the new rows.

Syntax

df.explode(column)

Deep Dive Course

explode() is designed for the common situation where a column holds a list, or other iterable, of values per row, and you actually need one row per individual item instead — every other column's value is simply repeated for each new row generated from that one original row's list, and the original row's index label is duplicated across the resulting rows too, unless you follow up with reset_index(). An empty list produces a single row with NaN for the exploded column, rather than disappearing entirely.

1Understanding df.explode()

explode() is designed for the common situation where a column holds a list, or other iterable, of values per row, and you actually need one row per individual item instead — every other column's value is simply repeated for each new row generated from that one original row's list, and the original row's index label is duplicated across the resulting rows too, unless you follow up with reset_index(). An empty list produces a single row with NaN for the exploded column, rather than disappearing entirely.

💡

After exploding a column, call reset_index(drop=True) if you need a clean, unique index afterward — explode() duplicates the original row's index label across every new row it creates from that row's list, rather than generating fresh unique labels.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))
localhost:3000

2Practical Example

Here is a real-world application of df.explode() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags").reset_index(drop=True))
localhost:3000

3Best Practices

Follow these guidelines when working with df.explode():

1. Use explode() to convert a column of lists into one row per individual list item, instead of manually looping and rebuilding the DataFrame

2. Follow explode() with reset_index(drop=True) if a clean, unique index is needed afterward

3. Check for and handle empty lists in the target column beforehand if a resulting NaN row for them isn't the desired behavior

⚠️

Tip: After exploding a column, call reset_index(drop=True) if you need a clean, unique index afterward — explode() duplicates the original row's index label across every new row it creates from that row's list, rather than generating fresh unique labels.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags").reset_index(drop=True))

Best Practices

  • Use explode() to convert a column of lists into one row per individual list item, instead of manually looping and rebuilding the DataFrame
  • Follow explode() with reset_index(drop=True) if a clean, unique index is needed afterward
  • Check for and handle empty lists in the target column beforehand if a resulting NaN row for them isn't the desired behavior

Interview Question

Why does the exploded result have two rows sharing the same original index label, before calling reset_index()?

Hint: Think about how explode() decides what index label each new row should get.

explode() creates one new row for each item in the original row's list, but it doesn't invent new, unique index labels for those new rows — it simply keeps the original row's index label attached to every new row that came from splitting apart that single original row's list. Since one original row with a two-item list produces two new rows, both of those new rows end up sharing that same original index label, which is why the exploded result commonly has duplicate index values until you explicitly call reset_index() to assign fresh, unique labels.

Exercises

MediumPractice using df.explode() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))

Frequently Asked Questions

Why does the exploded result have two rows sharing the same original index label, before calling reset_index()?

explode() creates one new row for each item in the original row's list, but it doesn't invent new, unique index labels for those new rows — it simply keeps the original row's index label attached to every new row that came from splitting apart that single original row's list. Since one original row with a two-item list produces two new rows, both of those new rows end up sharing that same original index label, which is why the exploded result commonly has duplicate index values until you explicitly call reset_index() to assign fresh, unique labels.

Related Functions

series-str-splitpd-meltdf-drop-duplicates