Pandas: Return Row with Max Value in Particular Column


You can use the following methods to return the row of a pandas DataFrame that contains the max value in a particular column:

Method 1: Return Row with Max Value

df[df['my_column'] == df['my_column'].max()]

Method 2: Return Index of Row with Max Value

df['my_column'].idxmax()

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 28, 20],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      28        9         9
7    H      20        4        12

Example 1: Return Row with Max Value

The following code shows how to return the row in the DataFrame with the max value in the points column:

#return row with max value in points column
df[df['points'] == df['points'].max()]

        team	points	assists	rebounds
6	G	28	9	9

The max value in the points column was 28, so the row that contained this value was returned.

Example 2: Return Index of Row with Max Value

The following code shows how to return only the index of the row with the max value in the points column:

#return row that contains max value in points column
df['points'].idxmax()

6

The row in index position 6 contained the max value in the points column, so a value of 6 was returned.

Related: How to Use idxmax() Function in Pandas (With Examples)

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Find the Max Value by Group in Pandas
How to Find the Max Value of Columns in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *