Running total per group
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
Return the running total of `revenue` for each rep, in the order the orders appear, as a Series aligned with the original rows. Each rep's total starts from zero.
What this exercise teaches
groupby("rep")["revenue"].cumsum() restarts the running total for every rep and returns the values in the original row order, so it slots straight back into the table as a new column. A plain cumsum mixes all reps together, and sum collapses to one number per rep.
A hint if you get stuck
cumsum is available directly on a grouped column, and it keeps one value per original row.
Try it now, free, no account needed
The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Analysis
- Rolling averagePro
- Month over month growthPro
- Running totalPro
- Rank with tiesPro
- Change from the previous periodPro
- Monthly totals with resamplePro
- Quartile bins with qcutPro
- Age bands with cutPro
- Classify with applyPro
- Top two orders per regionPro
- Monthly totals with to_periodPro
- Days since the previous orderPro