Python Pandas Series cummax() Function

Pandas Series cummax() Function:

Cumulative maximum is computed over a DataFrame or Series axis by the Pandas Series cummax() function, which returns a DataFrame or Series of the same size including the cumulative maximum.

Syntax:

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

Parameters

axis: This is optional. It indicates 0 or “index”, 1 or “columns.” Cumulative maximums are generated for each column if the value is 0 or ‘index’. Cumulative maximums are generated for each row if 1 or ‘columns’ is selected. 0 is the default.

skipna: This is optional. When determining the result, specify True to exclude NA/null values. True is the default value.

Return Value:

The cumulative maximum of scalar or a Series is returned by the cummax() function.

Python Pandas Series cummax() Function

Example1

Here, the cummax() function returns the cumulative maximum of values in a given series.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Apply cummax() function on the given series to get the cumulative maximum values of all the elements of the given series and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([12, 40, 10, 5, 15, 40, 12, 12])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply cummax() function on the given series to get the 
# cumulative maximum values of all the elements of the given series
# and print the result.
print("The cumulative maximum values of all the elements of the given series:")
print(gvn_series.cummax())

Output:

The given series is:
0    12
1    40
2    10
3     5
4    15
5    40
6    12
7    12
dtype: int64

The cumulative maximum values of all the elements of the given series:
0    12
1    40
2    40
3    40
4    40
5    40
6    40
7    40
dtype: int64

Example2

Here, the cummax() function is used on a specified series/columns of a DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe
  • Get the cumulative maximum values of the student_marks column of the dataframe using the cummax() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Get the cumulative maximum values of the student_marks column of the 
# dataframe using the cummax() function and print the result
print("The cumulative maximum values of the student_marks column of the dataframe:")
print(data_frme['student_marks'].cummax())

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The cumulative maximum values of the student_marks column of the dataframe:
virat     80
nick      80
jessy     80
sindhu    90
Name: student_marks, dtype: int64