AnalystPath

Total Spend per Diner

PandasEasyJunior level~10 min

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.

menu
dish_idunit_price
110
225
315
orders
order_iddish_iddiner_idservings
1110110
221011
331023
431022
521033

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 orders

Solve 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

Related Pandas questions