w3resource

Pandas Series: cumsum() function

Cumulative sum over a Pandas DataFrame or Series axis

The cumsum() function is used to get cumulative sum over a DataFrame or Series axis.

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

Syntax:

Series.cumsum(self, axis=None, skipna=True, *args, **kwargs)
Pandas Series cumsum image

Parameters:

Name Description Type/Default Value Required / Optional
axis The index or the name of the axis. 0 is equivalent to None or ‘index’. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
Required
skipna Exclude NA/null values. If an entire row/column is NA, the result will be NA. boolean
Default Value: True
Required
*args, **kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. Required

Returns: scalar or Series

Example - Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([3, np.nan, 4, -5, 0])
s

Output:

0    3.0
1    NaN
2    4.0
3   -5.0
4    0.0
dtype: float64
Pandas Series cumsum image

Example - By default, NA values are ignored:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([3, np.nan, 4, -5, 0])
s.cumsum()

Output:

0    3.0
1    NaN
2    7.0
3    2.0
4    2.0
dtype: float64

Example - To include NA values in the operation, use skipna=False:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([3, np.nan, 4, -5, 0])
s.cumsum(skipna=False)

Output:

0    3.0
1    NaN
2    NaN
3    NaN
4    NaN
dtype: float64

Previous: Cumulative product of a Pandas series
Next: Generate descriptive statistics in Pandas



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-cumsum.php