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 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
- One row per tag with explodePro
- Compare two price lists with suffixesPro