Pandas dataframe has cummax(), cummin(), cumsum(), cumprod() methods maintain the maximum, minimum, product and summation values found so far in an axis of the data frame and replace the original element with that value. The pandas example programs apply cumulative computation functions on various dataframe instances and provide the output.
Python pandas library provide several functions through the dataframe methods for performing cumulative computations which include cummax(), cummin(), cumsum() and cumprod().
The cummax() method of pandas dataframe looks for and maintains the maximum value encountered so far: either row wise(i.e., based on index) or column wise. When a new maximum is found it replaces the actual value with the new maximum.
The cummin() method maintains the minimum value encountered so far. When a new minimum is found it replaces the actual value with the new minimum.
The cumsum() method replaces the actual values of the dataframe along a given axis with the cumulative sum.
The cumprod() method replaces the actual values of the dataframe along a given axis with the cumulative product.
None values are considered as np.nan. The None values and np.nan values can be considered or ignored using the parameter skipna.
Example - Cumulative Maximum – axis=0, Ignore Nan
python
import pandas as pd
matrixData = {"a":[10,50,30,30,40],
"b":[60,None,100,90,80],
"c":[95,85,75,65,55],
"d":[5,25,45,35,15]
};
print("Input Dataframe:");
dataFrame = pd.DataFrame(data=matrixData);
maxval = dataFrame.cummax(axis=0);
print(dataFrame)
print("Dataframe returned by cummax:");
print(maxval)
Output
Output
Dataframe:
a b c d
0 10 60.0 95 5
1 50 NaN 85 25
2 30 100.0 75 45
3 30 90.0 65 35
4 40 80.0 55 15
Dataframe returned by cummax:
a b c d
0 10.0 60.0 95.0 5.0
1 50.0 NaN 95.0 25.0
2 50.0 100.0 95.0 45.0
3 50.0 100.0 95.0 45.0
4 50.0 100.0 95.0 45.0
Example - Cumulative Maximum – axis =0, Consider Nan
python
import pandas as pd
matrixData = {"w":[5,1,6,9,3],
"x":[2,7,None,9,4],
"y":[1,9,8,4,9],
"z":[4,3,4,1,8]
};
print("Dataframe:");
dataFrame = pd.DataFrame(data=matrixData);
maxval = dataFrame.cummax(axis=0, skipna=False);
print(dataFrame)
print("Dataframe returned by cummax:");
print(maxval)
Output
Output
Dataframe:
w x y z
0 5 2.0 1 4
1 1 7.0 9 3
2 6 NaN 8 4
3 9 9.0 4 1
4 3 4.0 9 8
Dataframe returned by cummax:
w x y z
0 5.0 2.0 1.0 4.0
1 5.0 7.0 9.0 4.0
2 6.0 NaN 9.0 4.0
3 9.0 NaN 9.0 4.0
4 9.0 NaN 9.0 8.0
Example - Cumulative Maximum – Axis = 1
python
import pandas as pd
matrixData = {"a":[101,27,157,41,165],
"b":[79,46,None,28,37],
"c":[56,21,96,73,9],
"d":[67,57,128,96,44]
};
print("Dataframe:");
dataFrame = pd.DataFrame(data=matrixData);
maxval = dataFrame.cummax(axis=1);
print(dataFrame)
print("Dataframe returned by cummax:");
print(maxval)
Output
Output
Dataframe:
a b c d
0 101 79.0 56 67
1 27 46.0 21 57
2 157 NaN 96 128
3 41 28.0 73 96
4 165 37.0 9 44
Dataframe returned by cummax:
a b c d
0 101.0 101.0 101.0 101.0
1 27.0 46.0 46.0 57.0
2 157.0 NaN 157.0 157.0
3 41.0 41.0 73.0 96.0
4 165.0 165.0 165.0 165.0
Example - Cumulative Minimum
python
import pandas as pd
frameData = {"a":[78,82,79,89,83],
"b":[89,81,86,82,79],
"c":[80,73,86,77,12],
"d":[90,92,None,88,77]
};
print("Dataframe:");
dataFrame = pd.DataFrame(data=frameData);
minval = dataFrame.cummin(axis=0);
print(dataFrame)
print("Dataframe returned by cummin:");
print(minval)
Output
Output
Dataframe:
a b c d
0 78 89 80 90.0
1 82 81 73 92.0
2 79 86 86 NaN
3 89 82 77 88.0
4 83 79 12 77.0
Dataframe returned by cummin:
a b c d
0 78.0 89.0 80.0 90.0
1 78.0 81.0 73.0 90.0
2 78.0 81.0 73.0 NaN
3 78.0 81.0 73.0 88.0
4 78.0 79.0 12.0 77.0
Example - Cumulative Sum
python
import pandas as pd
frameData = {"p1":[1.5,1.51,1.52,1.53,1.54],
"p2":[2,2.1,2.1,2.3,2.5],
"p3":[3,4,5,6,7],
"p4":[7,7.2,None,7.3,7.4]
};
print("Dataframe:");
dataFrame = pd.DataFrame(data=frameData);
sumval = dataFrame.cumsum(axis=0);
print(dataFrame)
print("Dataframe returned by cumsum:");
print(sumval)
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