Python
All Top Python Interview Questions#39
Compute Group-Wise Running Totals with cumsum() in Pandas
MediumPwCInterview Question #39
Asked at PwCGiven `transactions` (customer_id, txn_date, amount), calculate a running cumulative spend `cumulative_spend` for each customer over time.
Input Table: df
3 rows preview| customer_id | txn_date | amount |
|---|---|---|
| 1 | 2026-01-01 | 100 |
| 1 | 2026-01-05 | 200 |
| 2 | 2026-01-02 | 500 |
Expected Output Structure3 rows
| customer_id | txn_date | amount | cumulative_spend |
|---|---|---|---|
| 1 | 2026-01-01 | 100 | 100 |
| 1 | 2026-01-05 | 200 | 300 |
| 2 | 2026-01-02 | 500 | 500 |
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)…