Pandas DataFrame cummax() method with examples

Spread the love

The cummax() method in pandas return cumulative maximum over a DataFrame or Series axis. Each cell is populated with the maximum value seen so far.

Syntax –

dataframe.cummax(axis=None, skipna=True, *args, **kwargs)

Examples –

Let’s create a dataframe to work with.

import pandas as pd

data = {'Apple':[89, 89, 90, 110, 125, 84, 131, 123, 123, 140, 145, 145],
       'Orange': [46, 46, 50, 65, 63, 48, 110, 120, 60, 42, 47, 62],
       'Banana': [26, 30, 30, 25, 38, 22, 22, 36, 20, 27, 23, 34 ],
       'Mango': [80, 80, 90, 125, 130, 150, 140, 140, 135, 135, 80, 90]}

index = ['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec']
df = pd.DataFrame(data, index=index)
df

1 . Find the cumulative maximum value along the index axis –

By default cummax() method find the cumulative maximum value seen so far in a column. You can also set it explicitly by setting the axis parameter to axis=0 or index.

df.cummax(axis=0)

2 . Find cumulative maximum value along the column axis –

To find the cumulative maximum value seen so far in a row we set the axis parameter to axis=1 or columns

df.cummax(axis=1)

Related Posts –

  1. Pandas DataFrame cummin() method with examples
  2. Pandas DataFrame cumprod() method with examples
  3. Pandas DataFrame cumsum() method with examples

Rating: 1 out of 5.

Leave a Reply