What is the cumsum function in pandas?
The cumsum function in pandas returns a cumulative sum over a dataframe or series axis.
The return series or dataframe is of the same size as the original one, except with the cumulative sum.
The illustration below shows how the cumsum function works in pandas:
Syntax
The syntax of the cumsum function is as follows:
Series.cumsum(axis=None, skipna=True, *args, **kwargs)
Parameters
The table below describes each parameter of the cumsum function:
| Parameter | Description |
|---|---|
axis |
The axis of the cumulative sum. axis takes a bool value. 0 refers to column-wise sum, whereas 1 refers to row-wise. By default, axis is 0. |
skipna |
Determines if null values need to be excluded or not. By default, skipna is True. |
args |
Additional keywords have no effect but might be accepted for compatibility with NumPy. |
**kwargs |
Any additional arguments. |
All parameters are optional.
Return value
The cumsum function returns a series or dataframe with the cumulative sum at each index. The length of the return object is the same as the original series or dataframe.
Example
The code snippet below shows how the cumsum function is used in pandas:
import pandas as pdimport numpy as nps = pd.Series([2, np.nan, 5, -1, 0])print("Original Series")print(s)print('\n')print("Cumulative Sum-NA values are ignored by default")print(s.cumsum())print('\n')print("If skip_na is False")print(s.cumsum(skipna=False))print('\n')print("Original Dataframe")df = pd.DataFrame([[2.0, 1.0], [3.0, np.nan], [1.0, 0.0]], columns=list('AB'))print(df)print('\n')print("By default, cumulative sum")print(df.cumsum())print('\n')print("Sum by Axis = 1 (row-wise)")print(df.cumsum(axis=1))print('\n')
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved