Left-merge products with prices in pandas and keep unmatched products
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `left_join_prices(products, prices)`. Left-merge `products` (`product_id`, `product_name`) with `prices` (`product_id`, `price`) on `product_id` and return the result.
Requirements:
- Every row of `products` survives: unmatched products get `price` = `NaN`.
- The columns are exactly `['product_id', 'product_name', 'price']`.
- Sort by `product_id` ascending and call `reset_index(drop=True)`.
Example:
```python
products = pd.DataFrame({
'product_id': [3, 1, 2],
'product_name': ['Pen', 'Book', 'Mug'],
})
prices = pd.DataFrame({
'product_id': [1, 3],
'price': [9.99, 1.50],
})
left_join_prices(products, prices)
# product_id product_name price
# 0 1 Book 9.99
# 1 2 Mug NaN
# 2 3 Pen 1.50
```
```hint Join first, then tidy
Choose the join type from the first requirement. The merge keeps the row order of `products`, which is not the order the output asks for.
```
### Constraints & Assumptions
- `products` has the columns `product_id` and `product_name`; `prices` has `product_id` and `price`.
- `product_id` has the same dtype in both frames.
### What a Strong Answer Covers
- A left join on `product_id` so that every product row survives
- Missing prices appearing as `NaN`, and what that does to the dtype of `price`
- The exact column order, the ascending sort and the fresh index
- What happens when a key repeats in either frame
### Follow-up Questions
- What happens if `prices` contains the same `product_id` twice, and how would you catch it?
- How would you list only the products that have no price?
- How does a pandas merge treat missing `product_id` values, and how does that differ from a SQL join?
Overview: Write a pandas function that left-merges a products table with a prices table on product_id, keeps every product, and returns the rows sorted with a fixed column order and a fresh index. It tests join types, unmatched rows, duplicate keys and dtype handling.