Pandas DataFrame Min Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the min() method in Pandas DataFrame. This method helps us find the minimum value or values in a DataFrame over a specified axis. We will explore different examples to understand how to use this method effectively.

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/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) 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/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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/comments -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/with_statement -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/booleans -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/for_loops -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/lists -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/tuples -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/dictionaries -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/importing_modules -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/numerical_computing -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/data_analysis -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} python/build_in_functions -.-> lab-68666{{"`Pandas DataFrame Min Method`"}} end

Create a DataFrame

Let's start by creating a DataFrame using the Pandas library. We will use the pd.DataFrame() function to create a DataFrame object. Here's an example:

import pandas as pd

data = {'A': [10, 20, 30],
        'B': [40, 50, 60],
        'C': [70, 80, 90]}

df = pd.DataFrame(data)
print(df)

Now, let's execute this code to create and display our DataFrame.

Find the Minimum Values

Now that we have our DataFrame, let's use the min() method to find the minimum values. We can specify the axis as either 0 or 1. When axis=0, the method will find the minimum values for each column. When axis=1, the method will find the minimum values for each row.

## Find the minimum values for each column
min_values_column = df.min(axis=0)
print("Minimum values for each column:")
print(min_values_column)

## Find the minimum values for each row
min_values_row = df.min(axis=1)
print("\nMinimum values for each row:")
print(min_values_row)

Let's execute this code to find the minimum values and display the results.

Handling Null Values

The min() method also provides an option to handle null values. By default, it excludes null values when computing the minimum. However, we can include null values by setting the skipna parameter to False.

## Create a DataFrame with null values
data = {'A': [10, None, 30],
        'B': [40, 50, None],
        'C': [70, 80, 90]}

df = pd.DataFrame(data)
print(df)

## Find the minimum values including null values
min_values = df.min(axis=0, skipna=False)
print("\nMinimum values including null values:")
print(min_values)

Let's execute this code to create a DataFrame with null values and find the minimum values.

Summary

In this lab, we learned how to use the min() method in Pandas DataFrame. We can find the minimum values for each column or each row by specifying the appropriate axis. We also learned how to handle null values when finding the minimum. This method is helpful in analyzing and understanding the minimum values in our data.

Other Python Tutorials you may like