cswork / libraryOpen practice workspace

Coding questions · Roblox

Analyze Recent Orders Dataset with Python/pandas

This question evaluates proficiency in data manipulation and aggregation using Python/pandas, including extracting per-user maximums, computing overall summary statistics, and producing daily aggregates from time-stamped records.

orders

order_iduser_idpricecreated_at
110120.52024-01-01
210135.02024-01-03
310215.02024-01-02
410350.02024-01-04
510225.02024-01-05
Scenario

E-commerce analytics team needs quick Python insights on recent orders dataset.

Question

Using Python/pandas: a) For every user, return the order_id with the maximum price. b) Compute the overall average order price. c) For each calendar day, report total orders and average price.

Hints

Think groupby, idxmax, agg, reset_index.

Max-priced order per user

For each user, return the order_id and price of their highest-priced order. Break ties by earliest created_at, then smallest order_id.

Table orders

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
priceDECIMAL(10,2)NOT NULL
created_atDATENOT NULL

Sample data:

created_atorder_idpriceuser_id
2024-01-01120.5101
2024-01-03235101
2024-01-02315102
2024-01-04450103
2024-01-05525102

Expected output

One row per user with the order having the maximum price (ties broken by earliest date, then smallest order_id).

order_idpriceuser_id
235101
525102
450103

Hints

  1. Use ROW_NUMBER() partitioned by user_id
  2. Order by price DESC and tie-break by date then order_id

Overall average order price

Compute the average price across all orders in the table.

Table orders

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
priceDECIMAL(10,2)NOT NULL
created_atDATENOT NULL

Sample data:

created_atorder_idpriceuser_id
2024-01-01120.5101
2024-01-03235101
2024-01-02315102
2024-01-04450103
2024-01-05525102

Expected output

Single-row result with the overall average order price across all orders.

avg_price
29.1

Hints

  1. Use the AVG aggregate over the price column

Daily totals and average price

For each calendar day, report the total number of orders and the average order price. Order results by date ascending.

Table orders

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
priceDECIMAL(10,2)NOT NULL
created_atDATENOT NULL

Sample data:

created_atorder_idpriceuser_id
2024-01-01120.5101
2024-01-03235101
2024-01-02315102
2024-01-04450103
2024-01-05525102

Expected output

One row per day with total orders and average price for that day.

avg_pricecreated_datetotal_orders
20.52024-01-011
152024-01-021
352024-01-031
502024-01-041
252024-01-051

Hints

  1. Group by created_at to aggregate per day
  2. COUNT(*) for totals and AVG(price) for average