Pandas Series - cummax() function
The Pandas Series 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
Series.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 scalar or Series.
Example: using cummax() on a Series
In the example below, the cummax() function is used to get the cumulative maximum of values of a given series.
import pandas as pd import numpy as np x = pd.Series([10, 11, 12, 9, 13, 12, 10]) print("The Series contains:") print(x) #cumulative maximum of values in the series print("\nx.cummax() returns:") print(x.cummax())
The output of the above code will be:
The Series contains: 0 10 1 11 2 12 3 9 4 13 5 12 6 10 dtype: int64 x.cummax() returns: 0 10 1 11 2 12 3 12 4 13 5 13 6 13 dtype: int64
Example: using cummax() on selected series in a DataFrame
Similarly, the cummax() function can be applied on selected series/column of a given DataFrame. 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"] ) print("The DataFrame is:") print(info,"\n") #cumulative maximum on 'Salary' series print("info['Salary'].cummax() returns:") print(info['Salary'].cummax(),"\n")
The output of the above code will be:
The DataFrame is: 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
❮ Pandas Series - Functions