Implement a pandas function that inner-merges an orders table with a customers table on customer_id, fixes the column order, sorts by order_id and resets the index. It tests join semantics, how duplicate keys change row counts, and producing a clean DataFrame.
Implement `inner_orders(orders, customers)`. `orders` has `order_id` (int), `customer_id` (int) and `amount` (float). `customers` has `customer_id` (int) and `name` (str). Inner-merge them on `customer_id` and return the result.
The returned DataFrame must meet these requirements:
- Keep only rows whose key appears in both frames.
- Columns are exactly `['order_id', 'customer_id', 'amount', 'name']`, in that order.
- Rows are sorted by `order_id` ascending and the frame has a fresh `RangeIndex` (the old index is dropped).
Example:
```python
orders = pd.DataFrame({
'order_id': [3, 1, 2],
'customer_id': [10, 20, 99],
'amount': [5.0, 7.5, 1.0],
})
customers = pd.DataFrame({
'customer_id': [10, 20],
'name': ['Ana', 'Bo'],
})
inner_orders(orders, customers)
# order_id customer_id amount name
# 0 1 20 7.5 Bo
# 1 3 10 5.0 Ana
```
Order 2 is dropped because customer 99 does not exist in `customers`.
```hint Which rows survive
`merge` has a `how` argument that decides which keys are kept. After sorting, look at what the index labels are.
```
### What a Strong Answer Covers
- The join type that keeps only keys present in both frames, and why unmatched orders disappear.
- How the exact column order is guaranteed even if the inputs carry extra columns.
- Sorting by `order_id` and replacing the shuffled index labels with a fresh `RangeIndex`.
- What duplicate `customer_id` values in `customers` do to the row count, and how to guard against it.
### Follow-up Questions
- How would you keep all orders and mark those without a customer?
- What if `customer_id` is duplicated in `customers`?
- What happens if `customer_id` is stored as an integer in one frame and as a string in the other?
Overview: Implement a pandas function that inner-merges an orders table with a customers table on customer_id, fixes the column order, sorts by order_id and resets the index. It tests join semantics, how duplicate keys change row counts, and producing a clean DataFrame.
Implement inner_orders(orders, customers). orders has order_id (int), customer_id (int) and amount (float). customers has customer_id (int) and name (str). Inner-merge them on customer_id and return the result.
The returned DataFrame must meet these requirements:
Keep only rows whose key appears in both frames.
Columns are exactly
['order_id', 'customer_id', 'amount', 'name']
, in that order.
Rows are sorted by
order_id
ascending and the frame has a fresh
RangeIndex
(the old index is dropped).