+

Get the index of the minimum value in a DataFrame column

Let`s see how we can get the index of the minimum value in a DataFrame column.

Observe this dataset first. We will use the Weight and Salary columns of this data to get the index of the minimum values ​​from a specific column in the Pandas DataFrame.

# import pandas module

import pandas as pd 

 
# create data frame

df = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv "

 

df. head ( 10 )

Code # 1: Check the index for the minimum weight.

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( "nba.csv" )

 
# Returns the minimum weight index

df [[ `Weight` ]]. idxmin ()

Output:

We can check if the minimum value is present in the index or not.

 

 
# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( "nba.csv" )

 
Index # 140 to 154

df.iloc [ 140 : 155 ]

Output:

Code # 2: Let`s insert a new line with index 0 that has the minimum wage and then print the minimum wage.

# pandas module import

import pandas as pd 

 
# create data frame

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` : 99 }

  , index = [ 0 ])

  

df = pd.concat ([new_row, df]). .reset_index (drop = True )

df.head ( 5 )

Output:

Now Let`s check if the minimum wage is in the index 0 or not.

# Returns the minimum wage index

df [[ `Salary` ]]. idxmin ()

Output:

Get Solution for free from DataCamp guru