How to Use cummax and cummin in pandas for Cumulative Calculations
In pandas, use
cummax() to get the cumulative maximum values and cummin() for cumulative minimum values along a DataFrame column or Series. These functions return a new object showing the running max or min at each step.Syntax
The cummax() and cummin() methods are called on a pandas Series or DataFrame. They compute the cumulative maximum or minimum values respectively along the specified axis.
Series.cummax()orDataFrame.cummax(axis=0)Series.cummin()orDataFrame.cummin(axis=0)axis=0means operation is done column-wise (default for DataFrame)axis=1means operation is done row-wise
python
import pandas as pd # Syntax for Series series.cummax() series.cummin() # Syntax for DataFrame DataFrame.cummax(axis=0) DataFrame.cummin(axis=0)
Example
This example shows how to use cummax() and cummin() on a pandas Series and DataFrame to get cumulative maximum and minimum values.
python
import pandas as pd # Create a pandas Series s = pd.Series([3, 1, 4, 1, 5, 9, 2]) # Cumulative max and min on Series cummax_s = s.cummax() cummin_s = s.cummin() # Create a DataFrame df = pd.DataFrame({ 'A': [3, 1, 4, 1, 5], 'B': [9, 2, 6, 5, 3] }) # Cumulative max and min on DataFrame columns cummax_df = df.cummax() cummin_df = df.cummin() print('Original Series:\n', s) print('\nCumulative max Series:\n', cummax_s) print('\nCumulative min Series:\n', cummin_s) print('\nOriginal DataFrame:\n', df) print('\nCumulative max DataFrame:\n', cummax_df) print('\nCumulative min DataFrame:\n', cummin_df)
Output
Original Series:
0 3
1 1
2 4
3 1
4 5
5 9
6 2
dtype: int64
Cumulative max Series:
0 3
1 3
2 4
3 4
4 5
5 9
6 9
dtype: int64
Cumulative min Series:
0 3
1 1
2 1
3 1
4 1
5 1
6 1
dtype: int64
Original DataFrame:
A B
0 3 9
1 1 2
2 4 6
3 1 5
4 5 3
Cumulative max DataFrame:
A B
0 3 9
1 3 9
2 4 9
3 4 9
4 5 9
Cumulative min DataFrame:
A B
0 3 9
1 1 2
2 1 2
3 1 2
4 1 2
Common Pitfalls
One common mistake is misunderstanding that cummax() and cummin() operate along the specified axis and return cumulative results, not just the max or min of the entire data.
Also, when using DataFrames, forgetting to specify axis=1 if you want to compute row-wise cumulative max/min can lead to unexpected results.
python
import pandas as pd # DataFrame example df = pd.DataFrame({ 'X': [1, 3, 2], 'Y': [4, 1, 5] }) # Wrong: default axis=0 computes column-wise wrong_cummax = df.cummax() # Right: axis=1 computes row-wise cumulative max right_cummax = df.cummax(axis=1) print('Wrong (column-wise):\n', wrong_cummax) print('\nRight (row-wise):\n', right_cummax)
Output
Wrong (column-wise):
X Y
0 1 4
1 3 4
2 3 5
Right (row-wise):
0 1 4
1 3 3
2 2 5
dtype: int64
Quick Reference
- cummax(): Returns cumulative maximum values along the axis.
- cummin(): Returns cumulative minimum values along the axis.
- axis=0: Operate column-wise (default for DataFrame).
- axis=1: Operate row-wise.
- Works on both Series and DataFrames.
Key Takeaways
Use cummax() to get running maximum values and cummin() for running minimum values in pandas Series or DataFrames.
By default, these functions operate column-wise on DataFrames; use axis=1 for row-wise operations.
They return new objects showing cumulative results at each step, not just overall max or min.
Remember to choose the correct axis to avoid unexpected results.
These methods are useful for tracking trends or thresholds over time or sequence.