The functions cumsum(), cumprod(), cummax() and cummin() of the pandas.series class perform the operations cumulative sum, product, minimum and maximum. The python examples compute the cumulative results on sample data.
Number of participants so far arrived for a meeting, maximum rainfall over the century, the coldest temperature over decades - they all require computing of cumulative results like Cumulative Sum, Cumulative Maximum and Cumulative Minimum.
How many students have arrived so far, what is the current water level in a dam are some of the examples for cumulative sum.
As events occur over a period of time, cumulative sum gets incremented or decremented based on the nature of events.
When two students enter into the class, they add to the cumulative student count. In the similar way, when the water is released at a rate greater the incoming rate, the water level at the dam recedes.
The function Series.cumsum() computes the cumulative sum of the elements present in a pandas Series instance.
Example
python
# Python example program to compute the cumulative sum of
# a pandas.Series instance
import pandas as pds
# Forex inflow and outflow of a central bank in billions of US Dollars
forexReserves = [430, -20, -145, 500, -440];
forexSeries = pds.Series(forexReserves);
cumulativeReserves = forexSeries.cumsum();
print("Cumulative forex reserves in billions of US Dollars:");
print(cumulativeReserves);
Output
Output
Cumulative forex reserves in billions of US Dollars:
0 430
1 410
2 265
3 765
4 325
dtype: int64
Calculating cumulative maximum of a pandas.Series
The data structure pandas.Series is designed to store lengthy time series data as well as to perform computations on them.
The Series.cummax() finds the cumulative maximum of the elements present in a pandas.Series object.
The Python example code calculates the cumulative maximum of a city’s rainfall over a week.
Example
python
# Example Python program to find the cumulative maximum
# for the elements present in pandas.Series
import pandas as pds
# Rainfall data in inches
rainFall = [0.04, 0.0, 0.03, 0.17, 0.24];
# Create a pandas Series
rainSeries = pds.Series(rainFall);
# Find the cumulative maximum rainfall
cumMaxRain = rainSeries.cummax();
print("Daily rainfall:");
print(rainSeries);
print("Cumulative maximum rainfall over the week:");
print(cumMaxRain);
Series.cummin() function, finds the cumulative minimum for a pandas.Series().
The Python example finds the cumulative minimum for a city’s rainfall data over a period of one week.
Example
python
# Example Python program to find the cumulative minimum
# for the elements present in pandas.Series
import pandas as pds
# Python list containing temperature history of a city for seven days
cityTemp = [74,75,72,73,71,70];
# Create pandas series from the Pythonlist
tempSeries = pds.Series(cityTemp);
# Find the cumulative minimum temperature
cumMinTemp = tempSeries.cummin();
print("Daily temperature:");
print(tempSeries);
print("Cumulative minimum temperature over the week:");
print(cumMinTemp);
Using the Series.cumprod() cumulative product of a pandas.Series can be calculated.
Example
python
# Python example program to calculate the cumulative product
# of a pandas.series
import pandas as pds
# Create a pandas series
series = pds.Series([10, 20, 30, 40, 50, 60, 70]);
# Compute the cumulative product of the series
cumulativeProduct = series.cumprod();
print("Contents of the pandas series instance:");
print(series);
print("Cumulative product of the series:");
print(cumulativeProduct);
Output
Output
Contents of the pandas series instance:
0 10
1 20
2 30
3 40
4 50
5 60
6 70
dtype: int64
Cumulative product of the series:
0 10
1 200
2 6000
3 240000
4 12000000
5 720000000
6 50400000000
dtype: int64
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on