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.
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))2Practical Example
Here is a real-world application of df.explode() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags").reset_index(drop=True))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.
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"], "tags": [["admin", "user"], ["user"]]})
print(df.explode("tags"))