Articles

cummax function in pandas | cummax pandas example

cummax function in pandas | cummax pandas example


In this pandas tutorial, we will discuss about:

  • cummax function in pandas,
  • cummax pandas example

Before we start applying cummax function in pandas on the pandas DataFrame lets create one dataframe first.

 

DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.

 

Example: Create DataFrame

In this example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the building data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: DataFrame is created below

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Now lets see few cummax pandas example.


cummax function in pandas

cummax function in pandas or cummax() will return the cumulative maximum of values for the given dataframe.

Syntax:

dataframe.cummax(axis,skipna)

This will return the entire dataframe

Parameters:

  1. axis=0 specifies row and axis= 1 specifies column to get cumulative maximum along row/column
  2. skipna will take boolean values - True or False. If False,It will consider NaN values and If True,It will not  consider NaN values in cumulative maximum operation.

If we want to get the  cumulative maximum of values in a column for the given dataframe, we have to specify the column.

Syntax:

dataframe['column'].cummax()

where, dataframe is the input dataframe and column is the column name.

 

This will return the specified column cumulative maximum in the given dataframe.

 

Lets see few pandas cummax example.


Example 1cummax pandas example

Here in pandas cummax example, we will get the cumulative maximum for the entire dataframe and in a specific column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# get the cumulative maximum 
print(data.cummax())

print()

# get the cumulative maximum from length column
print(data['length'].cummax())

print()

# get the cumulative maximum from area column
print(data['area'].cummax())

Outputpandas cummax example result

       length  breadth  area
one       5.6     12.9    20
two       7.8     12.9    56
three     7.8     21.5    56
four      7.8     21.5    56

one      5.6
two      7.8
three    7.8
four     7.8
Name: length, dtype: float64

one      20
two      56
three    56
four     56
Name: area, dtype: int64

Lets see one more pandas cummax example with skipna.


Example 2: cummax pandas example

Here in cummax pandas example we will be dealing with skipna parameter.

import pandas as pd
import numpy as np

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[np.nan,7.8,4.5,np.nan],

                   "breadth":[12.9,4.5,21.5,np.nan],

                    "area":[2,np.nan,56,43]

                   },index=['one','two','three','four'])

# get the cumulative maximum with out nan values
print(data.cummax(skipna=True))

print()

# get the cumulative maximum  by considering nan values
print(data.cummax(skipna=False))

Outputcummax pandas example result

       length  breadth  area
one       NaN     12.9   2.0
two       7.8     12.9   NaN
three     7.8     21.5  56.0
four      NaN      NaN  56.0

       length  breadth  area
one       NaN     12.9   2.0
two       NaN     12.9   NaN
three     NaN     21.5   NaN
four      NaN      NaN   NaN

This wraps our session on how to use cummax function in pandas and cummax pandas example.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 08,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!