Running Totals

Python · GroupBy & SQL-Style Analytics

Theory

Cumulative methods walk down a column and keep a running result: cumsum() (running total), cumcount() (running row counter, starting at 0), cummax() (highest so far), cummin() and cumprod(). df['amount'].cumsum() on 100, 200, 50 gives 100, 300, 350. Because the result depends on row order, always sort first - typically by date - so 'running' means what you expect.

To restart the running value for each group, call the method on a groupby: df.groupby('city')['amount'].cumsum() gives a separate running total per city, aligned with the original rows - SQL's SUM(amount) OVER (PARTITION BY city ORDER BY order_date ROWS UNBOUNDED PRECEDING). groupby(...).cummax() tracks the best value so far per group, and dividing a running total by the group total (transform('sum')) gives cumulative percentage, used for Pareto (80/20) analysis. expanding().mean() gives a running average.

Why we use it

Year-to-date revenue, running balance of an account, best score so far and cumulative share of sales are everyday analytics. Cumulative methods compute them in one vectorised call, per group if needed.

When to use it

Use cumsum/cummax for running values over an ordered column, on a groupby to restart per partition. For a fixed-size moving window (last 3 days), use rolling instead; for running averages use expanding().mean().

How it works

  1. Sort by group key and date
  2. Start each group's running value at its first row
  3. Add (or compare) the next row's value
  4. Did the group key change? Yes: Restart the running value. No: Keep accumulating.
  5. Running total column aligned with rows

Syntax

df.sort_values('date')['col'].cumsum()
df.groupby('key')['col'].cumsum() / .cummax() / .cumcount()
s.expanding().mean()

Example

df = df.sort_values(['city', 'order_date'])
df['running'] = df.groupby('city')['amount'].cumsum()

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic