Get the index of maximum value in DataFrame column
Last Updated :
01 Nov, 2021
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).
Let’s see how can we get the index of maximum value in DataFrame column.
Observe this dataset first. We’ll use ‘Weight’ and ‘Salary’ columns of this data in order to get the index of maximum values from a particular column in Pandas DataFrame.
Python3
import pandas as pd
df.head( 10 )
|
Code #1: Check the index at which maximum weight value is present.
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
df[[ 'Weight' ]].idxmax()
|
Output:
We can verify whether the maximum value is present in index or not.
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
df.iloc[ 400 : 410 ]
|
Output:
Code #2: Let’s insert a new row at index 0, having maximum salary and then verify.
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
new_row = pd.DataFrame({ 'Name' : 'Geeks' , 'Team' : 'Boston' , 'Number' : 3 ,
'Position' : 'PG' , 'Age' : 33 , 'Height' : '6-2' ,
'Weight' : 189 , 'College' : 'MIT' , 'Salary' : 999999999 }
, index = [ 0 ])
df = pd.concat([new_row, df]).reset_index(drop = True )
df.head( 5 )
|
Output:
Now, let’s check if the maximum salary is present at index 0 or not.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...