Left join two pandas DataFrames on a shared index with DataFrame.join
Quick Overview
Write a pandas function that left joins an employees table to a salaries table on their shared emp_id index, keeps every employee, and returns the rows sorted by id. It tests DataFrame.join defaults, how unmatched rows and duplicate keys affect the result, and keeping the index intact.
Left join two pandas DataFrames on a shared index with DataFrame.join
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `join_on_index(employees, salaries)` that combines two DataFrames on their shared `emp_id` index. `employees` has `name` (str) and `dept` (str). `salaries` has a single `salary` (int) column. Return the resulting DataFrame.
The function must:
- Join via `DataFrame.join` as a left join: keep every employee, and give unmatched rows a `NaN` salary.
- Return columns exactly `['name', 'dept', 'salary']`.
- Sort by the `emp_id` index ascending and keep it as the index (no `reset_index`).
Example:
```text
employees:
name dept
emp_id
2 Alice Eng
1 Bob Sales
salaries:
salary
emp_id
1 50000
2 70000
join_on_index(employees, salaries):
name dept salary
emp_id
1 Bob Sales 50000
2 Alice Eng 70000
```
```hint Where the order comes from
A join keeps the row order of one of its inputs. Check which one, and whether that order already matches the required ascending `emp_id` order.
```
### What a Strong Answer Covers
- How `DataFrame.join` matches rows and which join type it uses by default.
- What an employee without a salary looks like in the output, including the dtype of `salary`.
- How the exact column order and the ascending `emp_id` order are guaranteed.
- Edge cases such as repeated `emp_id` values in `salaries` or index dtypes that do not match.
### Follow-up Questions
- How would you switch to an inner join, dropping employees with no salary?
- What if both frames shared a column name?
- What happens if `salaries` holds several rows for the same `emp_id`, and how would you catch it?
Overview: Write a pandas function that left joins an employees table to a salaries table on their shared emp_id index, keeps every employee, and returns the rows sorted by id. It tests DataFrame.join defaults, how unmatched rows and duplicate keys affect the result, and keeping the index intact.
Write join_on_index(employees, salaries) that combines two DataFrames on their shared emp_id index. employees has name (str) and dept (str). salaries has a single salary (int) column. Return the resulting DataFrame.
The function must:
Join via
DataFrame.join
as a left join: keep every employee, and give unmatched rows a
NaN
salary.
Sort by the
emp_id
index ascending and keep it as the index (no
reset_index
).
Example:
employees:
name dept
emp_id
2 Alice Eng
1 Bob Sales
salaries:
salary
emp_id
1 50000
2 70000
join_on_index(employees, salaries):
name dept salary
emp_id
1 Bob Sales 50000
2 Alice Eng 70000
What a Strong Answer Covers Guidance
How
DataFrame.join
matches rows and which join type it uses by default.
What an employee without a salary looks like in the output, including the dtype of
salary
.
How the exact column order and the ascending
emp_id
order are guaranteed.
Edge cases such as repeated
emp_id
values in
salaries
or index dtypes that do not match.
Follow-up Questions Guidance
How would you switch to an inner join, dropping employees with no salary?
What if both frames shared a column name?
What happens if
salaries
holds several rows for the same
emp_id
, and how would you catch it?