One row per tag with explode
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
Each post has its tags in one comma-separated string, such as "python,pandas". Return a DataFrame with the columns `post_id` and `tag`, one row per tag, with a fresh 0, 1, 2, ... index.
What this exercise teaches
explode makes one row per element of a list-valued column and repeats the other columns alongside. The new rows keep the original row label, so a post with three tags yields three rows labeled 2; reset_index(drop=True) gives them fresh positions. Without the split there is nothing to explode, and without explode you get lists in the cells.
A hint if you get stuck
First turn the string into a list with str.split, then call explode on that column, then reset the index.
Try it now, free, no account needed
The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Combining and reshaping
- Inner merge on a shared keyPro
- Left merge keeps every orderPro
- Merge on differently named keysPro
- Stack two tables with concatPro
- Merge twice, then aggregatePro
- Wide to long with meltPro
- Long to wide with pivotPro
- Columns into rows with stackPro
- Index level into columns with unstackPro
- Orders with no customer, via indicatorPro
- Label the source with concat keysPro
- Compare two price lists with suffixesPro