Get The Index of Rows Based on Column Value in Pandas Dataframe – Definitive Guide

Each row in the Pandas dataframe has an index.

You can get the index of rows based on column value in Pandas Dataframe using df.index[df[‘column_name’]==value].tolist() statement.

Basic Example

df.index[df['column_name']==value].tolist()

In this tutorial, you’ll learn how to get the index of rows based on column value in detail.

Sample Dataframe

This is the sample dataframe used throughout the tutorial.

It contains,

  • Rows with values for all columns
  • Rows with Empty or Missing Data for each column

Code

import pandas as pd
import numpy as np

data = {'Product': ['CPU','Speaker','Keyboard','Mouse', 'Monitor'],
        'Price': [22000,2500,3000,1500, None]
        }

df = pd.DataFrame(data, columns=['Product','Price'])

df

Dataframe Will Look Like

ProductPrice
0CPU22000.0
1Speaker2500.0
2Keyboard3000.0
3Mouse1500.0
4MonitorNaN

Using df.Index() Method

This section teaches you how to use the index attribute of the pandas dataframe.

To get the index,

  • Pass the condition to the index property to select the subset of rows based on the condition and return its index as a series
  • Use the tolist() method to convert the series of indexes into a list.

Code

The following code demonstrates how to get the rows’ index with the price = 1500 and convert it into a list.

df.index[df['Price'] == 1500].tolist()

Output

The output shows the index of the row that has the Price=1500.

    [3]

Get Index of Row Based on a Column Value Matching Single Condition

This section teaches you how to get the index of rows based on a column value matching a single condition.

Code

df.index[df['Price'] == 1500].tolist()

Output

    [3]

Get Index of Row Based on a Column Value Matching Multiple Condition

This section teaches you how to get the index of rows based on multiple conditions.

You can use the OR and AND to club multiple conditions.

  • The OR operator is denoted by the single pipe | symbol.
  • The AND operator is denoted by the single & symbol.

Code

df.index[(df['Price'] > 20000) & (df['Price'] < 50000)].tolist()

Output

  [0]

Get Index of Row Containing Missing Values

This section teaches how to get the row’s index containing missing values.

  • Use the isna() method to select the rows with missing values
  • Pass them to the index property
  • Convert them to a list using the tolist() method

Code

df.index[df['Price'].isna()].tolist()

Output

    [4]

Get Index of Row Containing String

This section teaches you how to get the index of rows containing a specific string.

  • select rows containing a specific string using the == operator.
  • Pass those rows to the index method
  • Use tolist() to convert the indexes to a list

Code

df.index[(df['Product'] == 'Monitor')].tolist()

Output

The product monitor is available in the fifth row; hence index is displayed as 4.

    [4]

Get Index of Row With Partial Match String

This section teaches you how to get the index of rows with a partial match string.

  • Select rows with a partial match string using the str.contains() method.
  • Pass them to the index method
  • Use tolist() to convert them to a list

Code

The code below demonstrates how to get the index of rows that contains the String M in the Product column.

df.index[df['Product'].str.contains('M')].tolist()

Output

    [3, 4]

Get Index of a First Row

You can get the first-row index using the index[0] statement

df.index[0]

Output

    0

Get Index of The Last Row

You can get the last-row index using the index[-1] statement.

df.index[-1]

Output

    4

Additional Resources

Leave a Comment