Pandas DataFrame Max Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the max() method in the Pandas DataFrame. This method is used to find the maximum value in a DataFrame. We will explore the syntax, parameters, and how to use this method with examples.

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-68658{{"`Pandas DataFrame Max Method`"}} python/lists -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/tuples -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/dictionaries -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/importing_modules -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/numerical_computing -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/data_analysis -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} python/build_in_functions -.-> lab-68658{{"`Pandas DataFrame Max Method`"}} end

Create a DataFrame

First, let's create a DataFrame in order to understand the max() method and how it works. We will use the following code:

import pandas as pd

df = pd.DataFrame({"A": [0, 52, 78], "B": [77, 45, 96], "C": [16, 23, 135], "D": [17, 22, 56]})

print(df)

This code creates a DataFrame with four columns: A, B, C, and D. Each column has three values.

Find Maximum Values Over Index Axis

Next, let's find the maximum values over the index axis (rows) of the DataFrame. We will pass axis=0 as a parameter to the max() method. See the code below:

max_values = df.max(axis=0)

print(max_values)

This code prints the maximum values over the index axis for each column.

Find Maximum Values Over Column Axis

Now, let's find the maximum values over the column axis (columns) of the DataFrame. We will pass axis=1 as a parameter to the max() method. See the code below:

max_values = df.max(axis=1)

print(max_values)

This code prints the maximum values over the column axis for each row.

Include Null Values

Sometimes our DataFrame may contain null values. To include null values when computing the maximum, we can use the skipna=False parameter. See the code below:

df = pd.DataFrame({"A": [0, None, 78], "B": [77, 45, None], "C": [16, 23, None], "D": [17, 22, 56]})

max_values = df.max(axis=0, skipna=False)

print(max_values)

This code prints the maximum values over the index axis, including null values.

Exclude Null Values

To exclude null values when computing the maximum, we can use the skipna=True parameter. See the code below:

df = pd.DataFrame({"A": [0, None, 78], "B": [77, 45, None], "C": [16, 23, None], "D": [17, 22, 56]})

max_values = df.max(axis=0, skipna=True)

print(max_values)

This code prints the maximum values over the index axis, excluding null values.

Summary

In this lab, we learned about the max() method in the Pandas DataFrame. We explored how to find the maximum values over the index axis and column axis, including or excluding null values. The max() method is useful for finding the highest values in a DataFrame and can be applied to various use cases. Make sure to review the syntax and parameters of the max() method to apply it effectively in your own projects.

Other Python Tutorials you may like