Total Spend per Diner
Problem
You are given two DataFrames. `orders` has columns `order_id`, `dish_id`, `diner_id`, and `servings`. `menu` has columns `dish_id` and `unit_price`. The cost of an order line is `servings * unit_price`. Return a DataFrame with columns `diner_id` and `total_spend`, where `total_spend` is the sum of all line costs for that diner. Sort the result by `total_spend` descending, then by `diner_id` ascending.
Input data
Example rows — the live problem includes the full dataset.
| dish_id | unit_price |
|---|---|
| 1 | 10 |
| 2 | 25 |
| 3 | 15 |
| order_id | dish_id | diner_id | servings |
|---|---|---|---|
| 1 | 1 | 101 | 10 |
| 2 | 2 | 101 | 1 |
| 3 | 3 | 102 | 3 |
| 4 | 3 | 102 | 2 |
| 5 | 2 | 103 | 3 |
Expected output
Your answer should return 3 rows with the columns diner_id, total_spend.
Starter code (Pandas (Python))
import pandas as pd
def total_spend_per_diner(orders, menu) -> pd.DataFrame:
# Your code here
return ordersSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock