Compute 3-Day Rolling Revenue Averages with Pandas

Quick Overview

This question evaluates proficiency with Python pandas for time-series data manipulation, specifically group-wise aggregation, rolling-window calculations, pivoting, and merging summary statistics.

Compute 3-Day Rolling Revenue Averages with Pandas

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Onsite

sales +------------+-----------+-------+---------+ | date | product_id| units | revenue | +------------+-----------+-------+---------+ | 2023-01-01 | 101 | 5 | 100.0 | | 2023-01-02 | 101 | 3 | 60.0 | | 2023-01-03 | 102 | 8 | 160.0 | | 2023-01-04 | 101 | 2 | 40.0 | | 2023-01-05 | 102 | 7 | 140.0 | +------------+-----------+-------+---------+ ##### Scenario Cleaning and aggregating daily sales data to compute rolling revenue trends per product. ##### Question Using Python/pandas on the sales table, compute the 3-day rolling average of revenue for each product_id, pivot the result so dates are rows and product_ids are columns, and include total units sold per product. ##### Hints Show imports, set date index, sort, groupby+rolling, pivot, merge totals; code should run end-to-end.

Quick Answer: This question evaluates proficiency with Python pandas for time-series data manipulation, specifically group-wise aggregation, rolling-window calculations, pivoting, and merging summary statistics.

|Home/Data Manipulation (SQL/Python)/Amazon
Amazon logo
Amazon
Aug 4, 2025, 10:55 AM
mediumData ScientistOnsiteData Manipulation (SQL/Python)
101
0

sales

+------------+-----------+-------+---------+ | date | product_id| units | revenue | +------------+-----------+-------+---------+ | 2023-01-01 | 101 | 5 | 100.0 | | 2023-01-02 | 101 | 3 | 60.0 | | 2023-01-03 | 102 | 8 | 160.0 | | 2023-01-04 | 101 | 2 | 40.0 | | 2023-01-05 | 102 | 7 | 140.0 | +------------+-----------+-------+---------+

Scenario

Cleaning and aggregating daily sales data to compute rolling revenue trends per product.

Question

Using Python/pandas on the sales table, compute the 3-day rolling average of revenue for each product_id, pivot the result so dates are rows and product_ids are columns, and include total units sold per product.

Hints

Show imports, set date index, sort, groupby+rolling, pivot, merge totals; code should run end-to-end.

Loading comments...