BasicsPandasJoinsMetaAmazonUber
Problem
Given orders(order_id, user_id, amount) and users(user_id, name), implement join_orders_with_users(orders, users) performing an inner join on user_id and returning columns ['order_id', 'name', 'amount'] sorted by order_id ascending.
Why It Matters: Relational joins in Pandas (pd.merge) combine transactional fact tables with user/product dimension tables.
Function Signature
import pandas as pd
def join_orders_with_users(orders: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Matched & Unmatched Orders
Input: Left Table:
| order_id | user_id | amount |
|---|---|---|
| 102.0 | 2.0 | 50.0 |
| 101.0 | 1.0 | 20.0 |
| 103.0 | 99.0 | 80.0 |
| Right Table: | ||
| user_id | name | |
| --- | --- | |
| 1 | Ava | |
| 2 | Ben |
Expected Output:
| order_id | name | amount |
|---|---|---|
| 101 | Ava | 20.0 |
| 102 | Ben | 50.0 |
Explanation: Order 103 (user_id=99) has no matching user and is dropped; remaining orders sort by order_id (101, 102).
Example 2: No Overlapping Keys
Input: Left Table:
| order_id | user_id | amount |
|---|---|---|
| 1.0 | 5.0 | 10.0 |
| Right Table: | ||
| user_id | name | |
| --- | --- | |
| 1 | Ava |
Expected Output:
| order_id | name | amount |
|---|
Explanation: Returns an empty DataFrame with columns ['order_id', 'name', 'amount'].
Constraints & Edge Cases
- Return columns
['order_id', 'name', 'amount']sorted byorder_idascending. - Exclude orders whose
user_idis not present inusers(inner join). - Sort by
order_idascending and reset index.
Constraints & Evaluation Criteria
- Return columns `['order_id', 'name', 'amount']` sorted by `order_id` ascending.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
2 matched + 1 orphan order
Expected Output / Criteria
2 joined rows