How to find index of value in Pandas dataframe

In this post, we are going to understand how to find an index of the value in pandas dataframe in Python that includes an index of all rows and rows in which column matches with specific conditions.

Dataframe.Index property


To find the index of rows in the pandas dataframe. index property is used. The dataframe. index returns the row label of the dataframe as an object. The individual property is to be accessed by using a loop.

Syntax

dataframe.index

Let us understand by using index property with loop and without loop.

Python Program to find indexes of all rows

#Python Program  to find indexes  of all rows
import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Math', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

indexes = df.index
print('index object of Dataframe:\n',indexes)

print('\n Access each index by using loop:')

for index in indexes:
    
  print(index)

output

index object of Dataframe:
 RangeIndex(start=0, stop=4, step=1)

Access each index by using loop:
0
1
2
3

1. df.loc[] to find Row index which column match value


The pandas dataframe. loc method is used to access the row and column by index(label) and column name that is passed by the column label(Marks) to df. loc[df[‘Marks’] = 100 and it will return the rows which satisfy the given condition.

Python Program Example

#python program to find Row index which column match value
import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

print('indexes of Dataframe:')  
print(df.loc[df['Marks'] == 100])
print((df.loc[df['Marks'] == 100]) & df.loc[df['Subject'] == 'Physic'))

Output

indexes of Dataframe:
    Name  Marks Subject
2    Max    100   Music
3  David    100  Physic

Most Viewed Post


2. df.index.values to Find an index of specific Value


To find the indexes of the specific value that match the given condition in the Pandas dataframe we will use df[‘Subject’] to match the given values and index. values to find an index of matched values.

The result shows us that rows 0,1,2 have the value ‘Math’ in the Subject column.

Python Program Example

import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Math', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

print('indexes of Dataframe:')  
print(df[df['Subject'] == 'Math'].index.values)





Output

indexes of Dataframe:
[0 1 2]

3. Find index of a row which column matches the value


In the below code example, Find the index of the row in which column matches the value of the single condition. The row 0 has value Name=’Jack’ and in case of multiple conditions rows 0,1 matches the given condition.

Python Program Example

#python program to find Row index which column match value
import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

print('index of Dataframe row which column match value:')

print('single condition:',df.index[df["Name"]=='Jack'].tolist())

print('Mutiple conditions:', df.index[(df["Subject"]=='Math')& (df['Marks']<=100)].tolist())





Output

index of Dataframe row which column match value:
single condition: [0]
Mutiple conditions: [0, 1]

4. index.tolist() to Find index of specific Value in Pandas dataframe


The df.Marks[df.Marks == 100].index is to find the index of matched value and finally using tolist() method to convert the indexes to list.In this example, the row 2,3 rows column marks has value of marks==100.

Python Program Example

import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

print('indexes of Dataframe:')  
print(df.Marks[df.Marks == 100].index.tolist())


Output

indexes of Dataframe:
[2, 3]

5. Find index of column in Padas Dataframe


The get_loc() function is used to find the index of any column in the Python pandas dataframe.We simply pass the column name to get_loc() function to find index.

Python Program Example

import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[99,98, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
  
df = pd.DataFrame(Student_dict)

print('index of Dataframe column:')  
print(df.columns.get_loc("Marks"))

Output

index of Dataframe column:
1

Summary

In this post, we have learned multiple ways of how to find index of value in Pandas dataframe with Python program code example that includes an index of all rows and row which column match with the specific condition.