# Cumulative Computations On Pandas Series

## Overview:

• 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.
• The functions Series.cumsum(), Series.cummax() and Series.cummin() of the pandas.Series class allow performing these cumulative operations on a pandas Series. The two-dimensional collection pandas.DataFrame also supports cumulative operations.

## Calculating cumulative sum of a pandas.Series:

• 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 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:

 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:

 # 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);

### Output:

 Daily rainfall: 0    0.04 1    0.00 2    0.03 3    0.17 4    0.24 dtype: float64 Cumulative maximum rainfall over the week: 0    0.04 1    0.04 2    0.04 3    0.17 4    0.24 dtype: float64

## Calculating cumulative minimum of a pandas.series:

• 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:

 # 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);

## Output:

 Daily temperature: 0    74 1    75 2    72 3    73 4    71 5    70 dtype: int64 Cumulative minimum temperature over the week: 0    74 1    74 2    72 3    72 4    71 5    70 dtype: int64

## Example:

 # 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:

 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