Pandas DataFrame Cummin Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the cummin() method in the Python pandas library. The cummin() method calculates the cumulative minimum over a DataFrame or Series axis. It returns a DataFrame or Series of the same size containing the cumulative minimum.

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/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`") subgraph Lab Skills python/booleans -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/lists -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/tuples -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/dictionaries -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/importing_modules -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/numerical_computing -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} python/data_analysis -.-> lab-68604{{"`Pandas DataFrame Cummin Method`"}} end

Importing the pandas library

First, we need to import the pandas library to use the cummin() method. We can do this by adding the following line of code at the beginning of our program:

import pandas as pd

Creating the DataFrame

Next, let's create a DataFrame that we can use for our examples. We can do this by using the pd.DataFrame() constructor and passing a dictionary of values. Here's an example:

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

This will create a DataFrame with four columns (A, B, C, D) and four rows.

Finding the cumulative minimum over the index axis

To find the cumulative minimum over the index axis, we can use the cummin() method on our DataFrame. We need to specify the axis parameter as 0 or 'index'. Here's an example:

df_cummin = df.cummin(axis=0)

This will calculate the cumulative minimum of each column over the index axis and store the result in the df_cummin DataFrame.

Finding the cumulative minimum over the column axis

To find the cumulative minimum over the column axis, we can use the cummin() method on our DataFrame. We need to specify the axis parameter as 1 or 'columns'. Here's an example:

df_cummin = df.cummin(axis=1)

This will calculate the cumulative minimum of each row over the column axis and store the result in the df_cummin DataFrame.

Handling null values

By default, the cummin() method excludes NA/null values. If an entire row/column is NA, the result will be NA. We can change this behavior by setting the skipna parameter to False. Here's an example:

df_cummin = df.cummin(skipna=False)

This will calculate the cumulative minimum of each column over the index axis, including any null values, and store the result in the df_cummin DataFrame.

Summary

In this lab, we learned how to use the cummin() method in the pandas library. We learned how to find the cumulative minimum over the index or column axis of a DataFrame. We also learned how to handle null values during the calculation. The cummin() method is useful for analyzing data and finding trends over time.

Other Python Tutorials you may like