Left Join Users With Order Totals (Fill Missing With 0)

Basics
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_idname
1Ava
2Ben
3Cara
Right Table:
user_idamount
------
1.025.0
1.035.0
3.010.0

Expected Output:

user_idnametotal_spend
1Ava60.0
2Ben0.0
3Cara10.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_idname
1Ava
2Ben
Right Table:
user_idamount
------

Expected Output:

user_idnametotal_spend
1Ava0.0
2Ben0.0

Explanation: Every user is preserved with total_spend = 0.0.

Constraints & Edge Cases

  • Return ['user_id', 'name', 'total_spend'] sorted by user_id ascending.
  • Aggregate orders by user_id first, then how='left' merge onto users.
  • Fill missing total_spend with 0.0.

Constraints & Evaluation Criteria

  • Return `['user_id', 'name', 'total_spend']` sorted by `user_id` ascending.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
Alice ($80), Bob ($0)
Expected Output / Criteria
[80.0, 0.0]