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.

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