Pandasdrill

Level 4: Combining and reshaping Exercise 49 of 63 Pro

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 exercise

More exercises in Combining and reshaping