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 pddata = {'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 Age0 Ben 201 Anna 272 Zoe 203 Tom 434 John 305 Steve 206 Becky 227 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 TomAge 43Name: 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 pddata = {'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 Age0 Ben 431 Anna 272 Zoe 203 Tom 434 John 305 Steve 206 Becky 227 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 Age0 Ben 433 Tom 43
and to get the indexes:
df[ df['Age'] == df['Age'].max() ].index
which returns
Int64Index([0, 3], dtype='int64')
