Implement a pandas function that inner-merges a sales table and a targets table on region and year, returns four fixed columns, and sorts the rows with a clean index. It tests composite-key joins, column selection, and how duplicate or mismatched keys change a merge.
Implement `merge_on_two_keys(sales, targets)` in pandas. `sales` has the columns `region` (str), `year` (int) and `revenue` (float). `targets` has the columns `region` (str), `year` (int) and `goal` (float). Inner-merge the two frames on both keys and return the result.
The returned DataFrame must meet these requirements:
- It is an inner merge with `on=['region', 'year']`.
- Its columns are exactly `['region', 'year', 'revenue', 'goal']`, in that order.
- Its rows are sorted by `region`, then `year`, both ascending, and it has a fresh `RangeIndex` (the old index is dropped).
Example:
```text
sales:
region year revenue
West 2021 100.0
East 2021 200.0
targets:
region year goal
East 2021 180.0
West 2021 90.0
merge_on_two_keys(sales, targets):
region year revenue goal
East 2021 200.0 180.0
West 2021 100.0 90.0
```
```hint Matching rows
Neither column identifies a row on its own. Check what `merge` accepts as its join key, and look at which index labels are left after a sort.
```
### What a Strong Answer Covers
- Joining on the composite key, so a row matches only when both `region` and `year` agree.
- Keeping only the key pairs present in both frames.
- Returning exactly the four required columns in the required order.
- Sorting by `region` then `year` and replacing the index without keeping the old labels.
- How duplicate key pairs or mismatched key types change the merged result.
### Follow-up Questions
- How do rows that match on `region` but not on `year` behave?
- What changes if unmatched rows must be preserved, and how would you tell which frame each row came from?
- What happens if `targets` holds two goals for the same region and year, and how would you catch that before it reaches a report?
Overview: Implement a pandas function that inner-merges a sales table and a targets table on region and year, returns four fixed columns, and sorts the rows with a clean index. It tests composite-key joins, column selection, and how duplicate or mismatched keys change a merge.
Implement merge_on_two_keys(sales, targets) in pandas. sales has the columns region (str), year (int) and revenue (float). targets has the columns region (str), year (int) and goal (float). Inner-merge the two frames on both keys and return the result.
The returned DataFrame must meet these requirements:
It is an inner merge with
on=['region', 'year']
.
Its columns are exactly
['region', 'year', 'revenue', 'goal']
, in that order.
Its rows are sorted by
region
, then
year
, both ascending, and it has a fresh
RangeIndex
(the old index is dropped).
Example:
sales:
region year revenue
West 2021 100.0
East 2021 200.0
targets:
region year goal
East 2021 180.0
West 2021 90.0
merge_on_two_keys(sales, targets):
region year revenue goal
East 2021 200.0 180.0
West 2021 100.0 90.0
What a Strong Answer Covers Guidance
Joining on the composite key, so a row matches only when both
region
and
year
agree.
Keeping only the key pairs present in both frames.
Returning exactly the four required columns in the required order.
Sorting by
region
then
year
and replacing the index without keeping the old labels.
How duplicate key pairs or mismatched key types change the merged result.
Follow-up Questions Guidance
How do rows that match on
region
but not on
year
behave?
What changes if unmatched rows must be preserved, and how would you tell which frame each row came from?
What happens if
targets
holds two goals for the same region and year, and how would you catch that before it reaches a report?