How to find a maximum value in a pandas dataframe column ?


Examples of how to find a maximum value in a pandas dataframe column:

Create a dataframe

Lets create for example a simple dataframe:

import pandas as pd

data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Becky','Bob'], 
        'Age':[20,27,20,43,30,20,22,21]}

df = pd.DataFrame(data)

df

which return:

    Name  Age
0    Ben   20
1   Anna   27
2    Zoe   20
3    Tom   43
4   John   30
5  Steve   20
6  Becky   22
7    Bob   21

Find the max value in the column Age

To find the maximum value in the column Age, a solution is to use the pandas function max:

df['Age'].max()

that gives the max value:

43

Find the index corresponding to the max value in the column Age

It is also possible to find the index corresponding to the max value in the column Age using the pandas function called idxmax

df['Age'].idxmax()

returns here

3

Then using the index above:

df.iloc[3,:]

we get

Name    Tom
Age      43
Name: 3, dtype: object

An example with multiple rows with a max value in the same column

Lets create a dataframe with two max values in the column Age:

import pandas as pd

data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Becky','Bob'], 
        'Age':[43,27,20,43,30,20,22,21]}

df = pd.DataFrame(data)

print(df)

gives

    Name  Age
0    Ben   43
1   Anna   27
2    Zoe   20
3    Tom   43
4   John   30
5  Steve   20
6  Becky   22
7    Bob   21

Then the function max:

df['Age'].max()

returns the max value which is

43

however idxmax:

df['Age'].idxmax()

returns only the first index:

0

To get rows with a max value in the column Age a solution is to do:

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

  Name  Age
0  Ben   43
3  Tom   43

and to get the indexes:

df[ df['Age'] == df['Age'].max() ].index

which returns

Int64Index([0, 3], dtype='int64')

References