How To Find the Maximum Element of All Columns/Rows in Pandas DataFrame

Working with Pandas DataFrames, you'll eventually face the situation where you need to find the maximum value for all of its columns or rows. Let's assume you already have a properly formatted DataFrame:

import pandas as pd 

df_data = { 
	"column1": [24, 9, 20, 24], 
	"column2": [17, 16, 201, 16] 
} 

df = pd.DataFrame(df_data) 
print(df)
   column1  column2
0       24       17
1        9       16
2       20      201
3       24       16

Finding the maximum element of each column of this DataFrame is pretty straightforward. All you need to do is to call the max() method:

max_elements = df.max()
print(max_elements)

This will give you a list of maximum elements for each column:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

column1 24 
column2 201 
dtype: int64

The same applies when you need to find the max element of each row of this DataFrame. The only difference is that you need to provide one additional argument to the max() method:

max_elements = df.max(axis=1)
print(max_elements)

This will give you the maximum value for each row of the df:

0 24 
1 16 
2 201 
3 24 
dtype: int64

Advice: If you want to get know more about Pandas DataFrames, consider reading our in-depth guide "Python with Pandas: DataFrame Tutorial with Examples".

Last Updated: July 20th, 2022
Was this helpful?
Project

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details

Ā© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms