Skip to Content

Pandas – Get Index of Rows whose Column Matches Value

In this tutorial, we will explore how to get the index of rows in a pandas dataframe whose column matches a specific value with the help of some examples.

Get specific row index

get specific row index in pandas

You can use df.index and boolean indexing to get the specific row indices that satisfy a given condition.
Alternatively, you can first filter the dataframe with the given condition (for example, where column matches a specific value) and then get the index of the resulting filtered dataframe.

The following is the syntax.

# get index of rows whose column matches a specific value
df.index[df['column_name']==value].tolist()

# get index of rows whose column matches a specific value
df[df['column_name']==value].index.tolist()

Examples

Let’s now look at some examples of using the above syntax.

First, we will create a pandas dataframe that we will be using throughout this tutorial.

import pandas as pd

# student data
data = {
   'Name': ['Tushar', 'Swastik', 'Sriti', 'Deepak', 'Supriya', 'Satya', 'Mamuni', 'Prangya'],
   'Class': [7, 10, 8, 10, 7, 6, 7, 8],
   'Age': [12, 15, 13, 15, 12, 11, 13, 13],
   'Gender': ['M', 'M', 'M', 'M', 'F', 'M', 'F', 'F']
}

# create pandas dataframe
df = pd.DataFrame(data)
# display the dataframe
df

Output:

the resulting dataframe with student information

Here, we created a dataframe with information about 8 Students. The dataframe has the columns – “Name”, “Class”, “Age”, and “Gender”.

Example 1: Get Index of Rows Whose Column Matches Value

Let’s get the index of rows where the value in the “Age” column is exactly 13.

# get index of rows where 'Age' column is equal to 13
df.index[df['Age']==13].tolist()

Output:

[2, 6, 7]

This tells us that the rows with index values 2, 6, and 7 have the value 13 in the Age column.

Alternatively, you can use –

# get index of rows where 'Age' column is equal to 13
df[df['Age']==13].index.tolist()

Output:

[2, 6, 7]

We get the same result as above.

Similarly, we can check for other conditions as well (not just equality check). For example, let’s get the indices of the rows where the “Age” is greater than 13.

#g et index of rows where 'Age' column is greater than 13
df.index[df['Age']>13].tolist()

Output:

[1, 3]

Example 2: Get Index of Rows Whose Column Matches a String

This is similar to the examples above. Let’s get the indices of all the rows where the “Gender” column is “F”.

# get index of rows where 'Gender' column is equal to 'F'
df.index[df['Gender']=='F'].tolist()

Output:

[4, 6, 7]

This tells us that the rows with index values 4, 6, and 7 have the value 'F' in the Gender column.

Example 3: Get Index of Rows with Multiple Conditions

You can also filter for multiple conditions and get the respective row indices that satisfy them.

# get index of rows where 'Age' is equal to 13 and 'Gender' is equal to 'M'
df.index[(df['Age']==13) & (df['Gender']=='M')].tolist()

Output:

[2]

Summary

In this tutorial, we looked at how to get the index of rows whose column matches a value in pandas with the help of some examples.

This tutorial covered examples where we –

  • Get the index of rows where a column is exactly equal to a given value.
  • Get the index of rows where a column is exactly equal to a given string.
  • Get the index of rows matching multiple conditions.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.