Running totals
Track a cumulative metric over time, such as total sales or accumulated scores.
Pandas · Module
Creates an expanding-window object for a DataFrame. Each subsequent position can aggregate all data available from the beginning through that position.
Calling expanding creates an Expanding object whose window starts at the first row and grows through each subsequent row. Apply an aggregation such as sum, mean, or max to calculate a statistic over all available observations at each position; min_periods controls when results begin appearing.
DataFrame.expanding(*min_periods=1*, *method='single'*)| Name | Type | Description |
|---|---|---|
min_periods | int, default 1 | Minimum number of observations in the window required to produce a value; otherwise the result is NaN. Default: 1. |
method | str {'single', 'table'}, default 'single' | Whether to operate per single column or row ('single') or over the entire object ('table'). Default: 'single'. Table mode is implemented only with engine='numba' in the method call. |
pandas.api.typing.Expanding, an expanding-window object for further calculations.
import pandas as pd
scores = pd.DataFrame({"score": [2, 4, 6, 8]})
scores["running_total"] = scores["score"].expanding(min_periods=2).sum()
print(scores)Creates a cumulative sum that starts after two valid observations are available.
Track a cumulative metric over time, such as total sales or accumulated scores.
Calculate a progressively updated average as new rows arrive.
Apply growing-window extrema to monitor the highest or lowest value seen so far.
It returns an Expanding object for subsequent expanding-window calculations. Each position can then aggregate all data available from the beginning through that position.
Follow-up: How would you delay output until enough observations exist? Pass a larger min_periods value to expanding before applying the aggregation.
min_periods sets the minimum number of observations required before a value is produced. With the default of 1, the first eligible observation can produce a result; otherwise the result is NaN.
Follow-up: What happens with missing data? The aggregation follows its own missing-value behavior, while min_periods determines whether enough observations are present.
method='single' applies the operation to each individual column or row, while method='table' applies it to the entire object. The documentation limits table mode to calls using engine='numba'.
Follow-up: When would table mode be relevant? Use it when a Numba-enabled operation needs to process the complete object rather than independent one-dimensional inputs.
pandas.api.typing.Expandingpandas.Series.expandingpandas.api.typing.Expanding.sumpandas.api.typing.Expanding.meanpandas.api.typing.Expanding.max