Pandasdrill

Level 4: Combining and reshaping Exercise 48 of 63 Pro

Label the source with concat keys

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

Stack `jan` on top of `feb` with pd.concat, using the keys "jan" and "feb" so the outer index level says which month each row came from, and name that level `month`. Keep the original row labels as the inner level.

What this exercise teaches

keys adds an outer index level with one label per input frame, giving a MultiIndex of (month, original label). That keeps the source of every row visible without adding a column, and reset_index() later turns the level into a column if you need one. Passing a dict does the same with the dict keys.

A hint if you get stuck

pd.concat takes keys (one label per frame) and names (labels for the new index levels).

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