BasicsPandasJoinsMetaAmazonUber
Problem
Given users(user_id, name) and orders(user_id, amount), implement user_total_spend(users, orders) returning every user with columns ['user_id', 'name', 'total_spend'] (filling users with no orders with 0.0), sorted by user_id ascending.
Why It Matters: Left-join + fillna(0.0) prevents survivorship bias—forgetting zero-spend users skews conversion rates and LTV models.
Function Signature
import pandas as pd
def user_total_spend(users: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Active & Zero-Order Users
Input: Left Table:
| user_id | name |
|---|---|
| 1 | Ava |
| 2 | Ben |
| 3 | Cara |
| Right Table: | |
| user_id | amount |
| --- | --- |
| 1.0 | 25.0 |
| 1.0 | 35.0 |
| 3.0 | 10.0 |
Expected Output:
| user_id | name | total_spend |
|---|---|---|
| 1 | Ava | 60.0 |
| 2 | Ben | 0.0 |
| 3 | Cara | 10.0 |
Explanation: Ava totals 60.0, Ben has no orders so gets 0.0, and Cara totals 10.0.
Example 2: Empty Orders Table
Input: Left Table:
| user_id | name |
|---|---|
| 1 | Ava |
| 2 | Ben |
| Right Table: | |
| user_id | amount |
| --- | --- |
Expected Output:
| user_id | name | total_spend |
|---|---|---|
| 1 | Ava | 0.0 |
| 2 | Ben | 0.0 |
Explanation: Every user is preserved with total_spend = 0.0.
Constraints & Edge Cases
- Return
['user_id', 'name', 'total_spend']sorted byuser_idascending. - Aggregate
ordersbyuser_idfirst, thenhow='left'merge ontousers. - Fill missing
total_spendwith0.0.
Constraints & Evaluation Criteria
- Return `['user_id', 'name', 'total_spend']` sorted by `user_id` ascending.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
Alice ($80), Bob ($0)
Expected Output / Criteria
[80.0, 0.0]