Pandas DataFrame Cummax Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the cummax() method in the pandas library. The cummax() method is used to calculate the cumulative maximum over a DataFrame or Series axis. It returns a DataFrame or Series of the same size, containing the cumulative maximum values.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/booleans -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/lists -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/tuples -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/dictionaries -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/importing_modules -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/numerical_computing -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/data_analysis -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} python/build_in_functions -.-> lab-68603{{"`Pandas DataFrame Cummax Method`"}} end

Import the pandas library

To use the cummax() method, we first need to import the pandas library. We can do this by running the following code:

import pandas as pd

Create a DataFrame

Next, we need to create a DataFrame on which we can apply the cummax() method. We can create a DataFrame using the pd.DataFrame() function and passing in a dictionary. Each key in the dictionary represents a column name, and the corresponding value is a list containing the column data.

df = pd.DataFrame({"A":[1, 2, 8, 4], "B":[9, 10, 7, 8], "C":[9, 10, 11, 12], "D":[13, 16, 15, 16]})

Apply the cummax() method

Now, we can apply the cummax() method to our DataFrame. The cummax() method takes three optional parameters: axis, skipna, and additional args/kwargs.

The axis parameter is used to specify the axis along which the cumulative maximum should be calculated. By default, it's set to 0 or 'index', which means the cumulative maximum will be calculated over the index axis. If set to 1 or 'columns', the cumulative maximum will be calculated over the column axis.

The skipna parameter is a boolean that determines whether NA/null values should be excluded from the calculation. If set to True, NA/null values will be excluded. If set to False, NA/null values will be included.

To apply the cummax() method, we can use the following code:

df_cummax = df.cummax(axis=0, skipna=True)

Print the result

Finally, we can print the result of the cummax() method to see the cumulative maximum values.

print(df_cummax)

Summary

In this lab, we learned how to use the cummax() method in the pandas library to calculate the cumulative maximum over a DataFrame or Series axis. We saw how to import the pandas library, create a DataFrame, apply the cummax() method with optional parameters, and print the result. The cummax() method is useful for analyzing trends and finding the highest values in a dataset over time. It can be a valuable tool in data analysis and decision-making processes.

Other Python Tutorials you may like