Largest order per customer

ranking

Largest order per customer

Amazon Pandas Interview Question

Amazon's customer insights team wants to see each customer's biggest purchase so far.

Using the orders DataFrame, find each customer's most valuable delivered order. Show the customer_id, the order_id and the order's value as order_value, rounded to 2 decimal places. An order's value is the quantity times the unit price, and no customer has two orders with the same top value. Assign the answer to result.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist
  • ML Engineer

ordersDataFrame30 rows

Column NameType
order_idint64
customer_idint64
order_datestr
categorystr
quantityint64
unit_pricefloat64
statusstr

ordersExample Input

order_idcustomer_idorder_datecategoryquantityunit_pricestatus
100015012024-05-01Books214.99delivered
100025022024-05-01Electronics1249delivered
100035032024-05-02Home319.5returned
100045012024-05-02Toys134.99delivered
100055042024-05-03Grocery64.25delivered
100065052024-05-03Electronics289.99canceled
100075022024-05-04Books122delivered
100085062024-05-04Home1129delivered
100095072024-05-05Grocery122.99delivered
100105032024-05-05Electronics159.99returned

Example Output

customer_idorder_idorder_value
5011000434.99
50210002249
5041000525.5
50610008129
5071000935.88

Explanation

Customer 501 had two delivered orders in the example, worth 29.98 and 34.99, so order 10004 is that customer's largest. Customer 503's orders were both returned, so that customer does not appear.

The example above is a small slice of the data. Your code runs against the full DataFrames.