Column-wise running totals
Compute running totals for each metric over ordered rows, such as daily sales or balances.
Pandas · Module
Computes cumulative sums along a DataFrame axis, returning an equally sized result. By default it accumulates down columns, skips missing values, and can optionally restrict the operation to numeric columns.
Call DataFrame.cumsum to replace each value with the running total along a selected DataFrame axis. By default it accumulates down columns, skips missing values while retaining the missing position, and can optionally restrict calculation to numeric columns.
DataFrame.cumsum(axis=0, skipna=True, numeric_only=False, *args, **kwargs)| Name | Type | Description |
|---|---|---|
axis | {0 or ‘index’, 1 or ‘columns’}, default 0 | The index or the name of the axis; 0, None, or 'index' accumulates down columns, while 1 or 'columns' accumulates across rows. Default: 0. |
skipna | bool, default True | Whether to exclude NA/null values. Default: True. |
numeric_only | bool, default False | Whether to include only float, integer, and Boolean columns. Default: False. |
*args | tuple | Additional arguments accepted for compatibility. |
**kwargs | dict | Additional keyword arguments accepted for compatibility. |
DataFrame or Series of the same size containing the cumulative sum.
None, and 'index' select accumulation down the index direction.'columns', to accumulate horizontally within rows.skipna=True omits missing values from the running total.numeric_only=True selects float, integer, and Boolean columns.skipna=True.numeric_only=True, non-numeric columns are excluded from the calculation.import pandas as pd
df = pd.DataFrame({"sales": [2, 3, 1], "returns": [1, None, 0]})
print(df.cumsum())
print(df.cumsum(axis=1))The first call accumulates values down each column; the second accumulates across each row.
Compute running totals for each metric over ordered rows, such as daily sales or balances.
Build per-record subtotals across several numeric fields.
Process mixed-type tables while excluding text and other non-numeric columns.
DataFrame.cumsum() return, and what is its default direction?It returns a DataFrame of the same shape containing running sums. The default axis is 0, which accumulates down each column.
Follow-up: How would you accumulate across each row? Use axis=1, also expressed as axis='columns'.
skipna affect missing data?skipna=True excludes missing values from the accumulation. A missing entry is therefore not added to the running total, while an entirely missing row or column produces missing output.
Follow-up: What changes with skipna=False? Missing values participate in propagation, so subsequent cumulative results can become missing after a missing value.
numeric_only control?numeric_only=True limits the operation to float, integer, and Boolean columns. The default is False, so the method does not request numeric-column filtering.
Follow-up: When is this useful? It prevents non-numeric columns from being included when a DataFrame contains mixed data types.
DataFrame.sumSeries.cumsumDataFrameGroupBy.cumsum