Merge on two key columns
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 row of `actual` is one store in one quarter, and `targets` has the target for every store and quarter. Add the matching `target` to every row of `actual` and return the columns `store`, `quarter`, `sales` and `target`. Row labels and row order do not matter.
What this exercise teaches
With a list in on=, two rows match only when every key column agrees. Merging on the store alone would pair each row with the targets of all quarters of that store, which multiplies the rows.
A hint if you get stuck
A row is identified by the store and the quarter together, so pass both names as a list: on=[...].
Try it now, free, no account needed
Your first 8 exercises 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 unlocks every exercise, timed mode and the certificate.
Open this exerciseMore exercises in Combining and reshaping
- Inner merge on a shared key
- Left merge keeps every order
- Merge on differently named keys
- Stack two tables with concat
- Merge twice, then aggregate
- Wide to long with melt
- Long to wide with pivot
- Columns into rows with stack
- Index level into columns with unstack
- Orders with no customer, via indicator
- Label the source with concat keys
- One row per tag with explode
- Compare two price lists with suffixes
- Merge a table with itself
- Outer merge: what is missing on each side