Series.cumsum() is used to find Cumulative sum of a series. In cumulative sum, the length of returned series is same as input and every element is equal to sum of all previous elements.
Syntax: Series.cumsum(axis=None, skipna=True)
axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation
skipna: Skips NaN addition for elements after the very next one if True.
Result type: Series
In this example, a series is created from a Python list using Pandas .Series() method. The list also contains a Null value and the skipna parameter is kept default, that is True.
3 7 NaN 14 16 16 dtype: float64
Cumulative sum is sum of current and all previous values. As shown in above output, the addition was done as follows
3 3+4 = 7 7+NaN = NaN 7+7 = 14 14+2 = 16 16+0 = 16
Example #2: skipna=False
In this example, a series is created just like in the above example. But the
skipna parameter is kept False. Hence NULL values won’t be ignored and it would be added every time after it’s occurrence.
0 1.0 1 21.0 2 34.0 3 NaN 4 NaN 5 NaN 6 NaN 7 NaN dtype: float64
Explanation: As it can be seen in output, all the values after first occurrence of NaN are also NaN since any number + NaN is also NaN.
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Python | Pandas series.cummax() to find Cumulative maximum of a series
- Python | Pandas Series.cummin() to find cumulative minimum of a series
- Python program to find Cumulative sum of a list
- Python | Pandas Series.str.find()
- Python | Pandas Series.cov() to find Covariance
- Python | Pandas Series.astype() to convert Data type of series
- Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
- Python | Pandas Series.nonzero() to get Index of all non zero values in a series
- Python | Pandas Series.sem()
- Python | Pandas Series.last()
- Python | Pandas Series.str.contains()
- Python | Pandas Series.mean()
- Python | Pandas Series.pow()
- Python | Pandas Series.str.len()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.