Pandas Aggregation and Filtering

Reported in interviews at The Voleon Group

Problem statement

You are given a list rows of transactions; each transaction is a 3-element list [customer_id, category, amount] where customer_id is an int, category is a str, and amount is a float. Load it into a pandas DataFrame with columns customer_id, category, amount.

Write top_spenders(rows, min_total) that, for each customer_id, sums amount across all of that customer's transactions, then keeps only the customers whose total spend is STRICTLY greater than min_total. Round each kept total to 2 decimals. Return a plain Python list [[customer_id, total_amount], ...] sorted by total_amount in DESCENDING order, ties broken by ASCENDING customer_id. Return [] when no customer clears the threshold.

Example: with rows = [[1, 'a', 50.0], [1, 'b', 30.0], [2, 'a', 100.0], [3, 'c', 10.0]] and min_total = 40, customer $1$ totals $80$ and customer $2$ totals $100$ (both $> 40$); customer $3$ totals $10$ (excluded). Result: [[2, 100.0], [1, 80.0]].

Show hint

Which pandas operation collapses many transactions into one number per customer?

Relevant roles: Data Scientist, Forward Deployed Engineer, ML Engineer and AI Engineer.

Related free problems