Pandas Tutorial Pandas References

Pandas DataFrame - cummax() function



The Pandas DataFrame cummax() function computes cumulative maximum over a DataFrame or Series axis and returns a DataFrame or Series of the same size containing the cumulative maximum.

Syntax

DataFrame.cummax(axis=None, skipna=True)

Parameters

axis Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', cumulative maximums are generated for each column. If 1 or 'columns', cumulative maximums are generated for each row. Default: 0
skipna Optional. Specify True to exclude NA/null values when computing the result. Default is True.

Return Value

Return cumulative maximum of Series or DataFrame.

Example: using cummax() column-wise on whole DataFrame

In the example below, a DataFrame info is created. The cummax() function is used to get the cumulative maximum of each column.

import pandas as pd
import numpy as np

info = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9]},
  index= ["2015", "2016", "2017", "2018", "2019"]
)

#displaying the dataframe
print(info,"\n")

#displaying the cumulative maximum
print("info.cummax() returns:")
print(info.cummax(),"\n")

#using skipna=False
print("info.cummax(skipna=False) returns:")
print(info.cummax(skipna=False))

The output of the above code will be:

      Salary  Bonus
2015      25   10.0
2016      24    8.0
2017      30    9.0
2018      28    NaN
2019      25    9.0 

info.cummax() returns:
      Salary  Bonus
2015      25   10.0
2016      25   10.0
2017      30   10.0
2018      30    NaN
2019      30   10.0 

info.cummax(skipna=False) returns:
      Salary  Bonus
2015      25   10.0
2016      25   10.0
2017      30   10.0
2018      30    NaN
2019      30    NaN

Example: using cummax() row-wise on whole DataFrame

To get the row-wise cumulative maximum, the axis parameter can be set to 1.

import pandas as pd
import numpy as np

info = pd.DataFrame({
  "2016": [25, 24, 30, 28, 25],
  "2017": [18, 20, 25, np.nan, 28],
  "2018": [25, 24, 25, 30, 25]},
  index= ["P1", "P2", "P3", "P4", "P5"]
)

#displaying the dataframe
print(info,"\n")

#displaying the cumulative maximum
print("info.cummax(axis=1) returns:")
print(info.cummax(axis=1),"\n")

#using skipna=False
print("info.cummax(axis=1, skipna=False) returns:")
print(info.cummax(axis=1, skipna=False))

The output of the above code will be:

    2016  2017  2018
P1    25  18.0    25
P2    24  20.0    24
P3    30  25.0    25
P4    28   NaN    30
P5    25  28.0    25 

info.cummax(axis=1) returns:
    2016  2017  2018
P1  25.0  25.0  25.0
P2  24.0  24.0  24.0
P3  30.0  30.0  30.0
P4  28.0   NaN  30.0
P5  25.0  28.0  28.0 

info.cummax(axis=1, skipna=False) returns:
    2016  2017  2018
P1  25.0  25.0  25.0
P2  24.0  24.0  24.0
P3  30.0  30.0  30.0
P4  28.0   NaN   NaN
P5  25.0  28.0  28.0

Example: using cummax() on selected column

Instead of whole DataFrame, the cummax() function can be applied on selected columns. Consider the following example.

import pandas as pd
import numpy as np

info = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9],
  "Others": [5, 4, 7, 5, 8]},
  index= ["2015", "2016", "2017", "2018", "2019"]
)

#displaying the dataframe
print(info,"\n")

#cumulative maximum on single column
print("info['Salary'].cummax() returns:")
print(info['Salary'].cummax(),"\n")

#cumulative maximum on multiple column
print("info[['Salary', 'Others']].cummax() returns:")
print(info[['Salary', 'Others']].cummax(),"\n")

The output of the above code will be:

      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5
2019      25    9.0       8 

info['Salary'].cummax() returns:
2015    25
2016    25
2017    30
2018    30
2019    30
Name: Salary, dtype: int64 

info[['Salary', 'Others']].cummax() returns:
      Salary  Others
2015      25       5
2016      25       5
2017      30       7
2018      30       7
2019      30       8 

❮ Pandas DataFrame - Functions