Home » How to Find the Max Value of Columns in Pandas

How to Find the Max Value of Columns in Pandas

by Erma Khan

Often you may be interested in finding the max value of one or more columns in a pandas DataFrame. Fortunately you can do this easily in pandas using the max() function.

This tutorial shows several examples of how to use this function.

Example 1: Find the Max Value of a Single Column

Suppose we have the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
                   'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 8, 5, 7, 6, 9, 9, 5],
                   'rebounds': [np.nan, 8, 10, 6, 6, 9, 6, 10, 10, 7]})

#view DataFrame 
df


        player	points	assists	rebounds
0	A	25	5	NaN
1	B	20	7	8.0
2	C	14	7	10.0
3	D	16	8	6.0
4	E	27	5	6.0
5	F	20	7	9.0
6	G	12	6	6.0
7	H	15	9	10.0
8	I	14	9	10.0
9	J	19	5	7.0

We can find the max value of the column titled “points” by using the following syntax:

df['points'].max()

27

The max() function will also exclude NA’s by default. For example, if we find the max of the “rebounds” column, the first value of “NaN” will simply be excluded from the calculation:

df['rebounds'].max()

10.0

The max of a string column is defined as the highest letter in the alphabet:

df['player'].max()

'J'

Example 2: Find the Max of Multiple Columns

We can find the max of multiple columns by using the following syntax:

#find max of points and rebounds columns
df[['rebounds', 'points']].max()

rebounds    10.0
points      27.0
dtype: float64

Example 3: Find the Max of All Columns

We can find also find the max of all numeric columns by using the following syntax:

#find max of all numeric columns in DataFrame
df.max()

player       J
points      27
assists      9
rebounds    10
dtype: object

Example 4: Find Row that Corresponds to Max

We can find also return the entire row that corresponds to the max value in a certain column. For example, the following syntax returns the entire row that corresponds to the player with the max points:

#return entire row of player with the max points
df[df['points']==df['points'].max()]

	player	points	assists	rebounds
4	E	27	5	6.0

If multiple rows have the same max value, each row will be returned. For example, suppose player D also scored 27 points:

#return entire row of players with the max points
df[df['points']==df['points'].max()]


        player	points	assists	rebounds
3	D	27	8	6.0
4	E	27	5	6.0

You can find the complete documentation for the max() function here.

Related Posts