Exploding Lists into Rows (explode)
explode() unpacks a column of lists so that every element gets its own row, copying the other column values down to match — turning one wide, nested row into several tidy ones.
Learn Exploding Lists into Rows (explode) in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll learn df.explode("col") , how to split strings into lists first, and how to reverse the operation with groupby().agg(list) .
What You'll Learn in This Lesson
1 The Basic Explode — One Row Per List Item
Data often arrives with multiple values crammed into a single cell — an order with several items, a user with several tags. df.explode("col") spreads that list out: each element becomes its own row, and the values in the other columns are duplicated so every new row stays complete.
2 Split a String, Then Explode
Very often the "list" is really a delimited string like "math,art" . explode() will not split that for you — it only unpacks real list-like values. The two-step recipe is: turn the string into a list with .str.split(",") , then explode the resulting list column.
3 The Inverse — groupby().agg(list)
Exploding is reversible. To collapse the tidy rows back into one row per original entity, group by the identifying column and aggregate the item column with the built-in list function. This gathers each group's values back into a single list.
! Common Errors (And How to Fix Them)
A raw string is not a list, so each character is not split out — the row is returned unchanged:
🎯 Mini Challenge: Tag Popularity
Each post has pipe-separated tags. Find out which tags appear most often.
- Split the tags string on "|"
- Explode into one tag per row
- Count tags with value_counts()
❓ Frequently Asked Questions
Lesson complete — nested data is no longer scary!
You can unpack list columns with explode() , split strings into lists first, reset labels with ignore_index=True , and rebuild the nested shape with groupby().agg(list) .
🚀 Up next: Cross-Tabulation — count combinations of two categories with pd.crosstab .
Practice quiz
What does df.explode('items') do?
- Deletes the items column
- Gives every list element its own row
- Joins lists into one string
- Counts the items
Answer: Gives every list element its own row. explode unpacks a list cell so each element becomes its own row.
When a row's list is exploded, what happens to the other columns?
- They are set to NaN
- They are dropped
- They are summed
- Their values are duplicated down to match
Answer: Their values are duplicated down to match. The other column values are copied so every new row stays complete.
If column 'items' holds [['pen','pad'],['mug'],['hat','scarf','gloves']], how many rows does explode produce?
- 3
- 9
- 6
- 1
Answer: 6. 2 + 1 + 3 = 6 rows after exploding.
What index labels result from exploding rows 0, 1, 2 with 2, 1, 3 items?
- 0, 0, 1, 2, 2, 2
- 0, 1, 2, 3, 4, 5
- 0, 1, 2
- All zeros
Answer: 0, 0, 1, 2, 2, 2. explode keeps each original label, so you get 0, 0, 1, 2, 2, 2.
How do you give the exploded result a clean 0..n index?
- reset_index=True
- ignore_index=True
- new_index=True
- fresh=True
Answer: ignore_index=True. Pass ignore_index=True to get a fresh RangeIndex.
What happens if you explode a column of plain comma-separated strings?
- Each character becomes a row
- It raises an error
- It splits on commas automatically
- Nothing useful — the row is returned unchanged
Answer: Nothing useful — the row is returned unchanged. explode only splits list-like values, not raw strings; the row stays as one.
What is the correct two-step recipe to explode 'math,art'?
- .str.split(',') then explode
- explode then .str.split(',')
- astype(list) then sum
- groupby then explode
Answer: .str.split(',') then explode. First .str.split(',') makes a real list, then explode flattens it.
How do you reverse an explode and collapse rows back into lists?
- df.implode()
- df.unstack()
- id
- items
Answer: id. Group by the identifying column and aggregate with list to rebuild the nested shape.
After splitting tags on '|' and exploding ['python|data','data|viz|python','ml'], how many times does 'python' appear in value_counts()?
- 1
- 2
- 3
- 0
Answer: 2. python appears in posts 1 and 2, so its count is 2.
Which form is best for filtering, counting, and joining individual items?
- The nested list form
- The exploded long form
- A pivot table
- A JSON string
Answer: The exploded long form. The exploded (long) form gives one item per row, ideal for filtering and counting.
Continue this course
- Previous: Interpolation & Filling Gaps
- Next: Cross-Tabulation (crosstab)