Pandasdrill

Level 4: Combining and reshaping Exercise 60 of 102

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 exercise

More exercises in Combining and reshaping