Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.cumsum

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.

How it works

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.

Syntax

DataFrame.cumsum(axis=0, skipna=True, numeric_only=False, *args, **kwargs)

Parameters

NameTypeDescription
axis{0 or ‘index’, 1 or ‘columns’}, default 0The index or the name of the axis; 0, None, or 'index' accumulates down columns, while 1 or 'columns' accumulates across rows. Default: 0.
skipnabool, default TrueWhether to exclude NA/null values. Default: True.
numeric_onlybool, default FalseWhether to include only float, integer, and Boolean columns. Default: False.
*argstupleAdditional arguments accepted for compatibility.
**kwargsdictAdditional keyword arguments accepted for compatibility.

Returns

DataFrame or Series of the same size containing the cumulative sum.

Key points

  • The method computes a running sum along one DataFrame axis.
  • For a DataFrame, the result preserves the original dimensions.
  • Axis 0, None, and 'index' select accumulation down the index direction.
  • Use axis 1, or 'columns', to accumulate horizontally within rows.
  • The default skipna=True omits missing values from the running total.
  • numeric_only=True selects float, integer, and Boolean columns.

Common mistakes

  • A completely missing row or column remains missing even when skipna=True.
  • With numeric_only=True, non-numeric columns are excluded from the calculation.

Example: Cumulative sums by column or row

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.

When to use it

Column-wise running totals

Compute running totals for each metric over ordered rows, such as daily sales or balances.

Row-wise accumulation

Build per-record subtotals across several numeric fields.

Numeric-column filtering

Process mixed-type tables while excluding text and other non-numeric columns.

Interview questions

QWhat does 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'.

QHow does 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.

QWhat does 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.sum
  • Series.cumsum
  • DataFrameGroupBy.cumsum

Source: pandas.DataFrame.cumsum — pandas 3.0.6 documentation. Checked 2026-10-15.