KatInterview

Extract User Metrics and Daily Summaries from Order Data

SQL & DataModel answer
MediumModel answerSQL & Datapandasaggregationgroupby

Last updated

Perform three pandas aggregations: identify each user's highest-value order, calculate mean order value, and generate per-day order statistics.

You have a DataFrame representing e-commerce orders with the following structure:

| order_id | user_id | price | created_at |
|----------|---------|-------|------------|
| 1        | 101     | 20.5  | 2024-01-01 |
| 2        | 101     | 35.0  | 2024-01-03 |
| 3        | 102     | 15.0  | 2024-01-02 |
| 4        | 103     | 50.0  | 2024-01-04 |
| 5        | 102     | 25.0  | 2024-01-05 |

Using Python and pandas, write functions to compute the following:

Part A: For each user, identify the complete order record (order_id, user_id, price) corresponding to their most expensive purchase.

Part B: Calculate the mean price across all orders in the dataset.

Part C: Generate a summary showing, for each calendar date, the count of orders placed and the mean price on that date.

Your solution should handle typical data issues like missing values and timestamp normalization.

Hints

Hint 1

For finding rows with maximum values within groups, consider idxmax() combined with loc[] indexing

Hint 2

When aggregating by date, ensure timestamps are normalized to remove time-of-day information

Hint 3

Named aggregation in agg() lets you specify both the column and the function while controlling output column names