Python
All Top Python Interview Questions#43
Calculate Rolling 30-Day Moving Averages in Pandas
MediumFlipkartInterview Question #43
Asked at FlipkartGiven a daily sales DataFrame with a DatetimeIndex and `revenue`, compute a 30-day rolling mean `rolling_mean_30d` and rolling standard deviation `rolling_std_30d`.
Input Table: df
10 rows preview| date | revenue |
|---|---|
| 2026-01-01 | 10000 |
| 2026-01-02 | 12000 |
| 2026-01-03 | 11000 |
| 2026-01-04 | 13000 |
| 2026-01-05 | 9500 |
| 2026-01-06 | 10500 |
| 2026-01-07 | 11500 |
| 2026-01-08 | 12500 |
| 2026-01-09 | 10800 |
| 2026-01-10 | 11200 |
Expected Output Structure10 rows
| date | revenue | rolling_mean_30d | rolling_std_30d |
|---|---|---|---|
| 2026-01-01 | 10000 | NULL | NULL |
| 2026-01-02 | 12000 | NULL | NULL |
| 2026-01-03 | 11000 | NULL | NULL |
| 2026-01-04 | 13000 | NULL | NULL |
| 2026-01-05 | 9500 | NULL | NULL |
| 2026-01-06 | 10500 | NULL | NULL |
| 2026-01-07 | 11500 | 11071.43 | 1205.15 |
| 2026-01-08 | 12500 | 11250 | 1224.74 |
| 2026-01-09 | 10800 | 11200 | 1155.42 |
| 2026-01-10 | 11200 | 11200 | 1089.34 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Starting the real Python + pandas runtime (one-time download)…