Pandas DataFrame cummax() Method
In this tutorial, we will learn the Python pandas DataFrame.cummax()
method. It gives a cumulative maximum over a DataFrame or Series axis. It returns a DataFrame or Series of the same size containing the cumulative maximum
.
The below shows the syntax of the Python pandas DataFrame.cummax()
method.
Syntax
DataFrame.cummax(axis=None, skipna=True, *args, **kwargs)
Parameters
axis: {0 or ‘index’, 1 or ‘columns’}, default 0. The index or the name of the axis. 0 is equivalent to None or ‘index’.
skipna: bool, default True. Exclude NA/null values. If an entire row/column is NA, the result will be NA.
*args, **kwargs: Additional keywords have no effect but might be accepted for compatibility with NumPy.
Example 1: Finding the cumulative maximum of the DataFrame
The below example shows how to find the cumulative maximum of the DataFrame over the index axis using the DataFrame.cummax()
method.
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 2, 8, 4], "B":[9, 10, 7, 8], "C":[9, 10, 11, 12],"D":[13, 16, 15, 16]})
print(df)
print("-----------Finding cumulative maximum-------")
print(df.cummax(axis = 0))
Once we run the program we will get the following output.
A B C D
0 1 9 9 13
1 2 10 10 16
2 8 7 11 15
3 4 8 12 16
-----------Finding cumulative maximum-------
A B C D
0 1 9 9 13
1 2 10 10 16
2 8 10 11 16
3 8 10 12 16
Example 2: Finding the cumulative maximum of the DataFrame over the column
axis
The below example shows how to find the cumulative maximum of the DataFrame over the column axis using the DataFrame.cummax()
method.
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 2, 8, 4], "B":[9, 10, 7, 8], "C":[9, 10, 11, 12],"D":[13, 16, 15, 16]})
print(df)
print("-----------Finding cumulative maximum-------")
print(df.cummax(axis = 1))
Once we run the program we will get the following output.
A B C D
0 1 9 9 13
1 2 10 10 16
2 8 7 11 15
3 4 8 12 16
-----------Finding cumulative maximum-------
A B C D
0 1 9 9 13
1 2 10 10 16
2 8 8 11 15
3 4 8 12 16
Example 3: Finding the cumulative maximum of the DataFrame
The below example shows how to find the cumulative maximum of the DataFrame with null values over the index axis using the DataFrame.cummax()
method.
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 2, 8, 4], "B":[9, None, 7, 8], "C":[9, 10, None, 12],"D":[None, 16, 15, 16]})
print(df)
print("-----------Finding cumulative maximum-------")
print(df.cummax(skipna=False))
Once we run the program we will get the following output.
A B C D
0 1 9.0 9.0 NaN
1 2 NaN 10.0 16.0
2 8 7.0 NaN 15.0
3 4 8.0 12.0 16.0
-----------Finding cumulative maximum-------
A B C D
0 1 9.0 9.0 NaN
1 2 NaN 10.0 NaN
2 8 NaN NaN NaN
3 8 NaN NaN NaN
Conclusion
In this tutorial, we learned the Python pandas DataFrame.cummax()
method. We learned the syntax, parameters and by solving examples we understood the DataFrame.cummax()
method.