pythonbeginner

Pandas Explode List Column

Explode a column containing lists into separate rows, useful for normalising one-to-many relations.

python
import pandas as pd

df = pd.DataFrame({'order_id':[1,2,3],'items':[['pen','notebook'],['stapler'],['paper','tape','pen']]})

expanded = (
    df.explode('items')
      .rename(columns={'items':'item'})
      .reset_index(drop=True)
)
print(expanded)

Use Cases

  • array column expansion
  • data normalisation
  • one-to-many flattening

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.