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.

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

If You’re in Hurry…

You can use the df.index attribute to get the index and use the tolist() method to convert the indexes into a list.

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

If You Want to Understand Details, Read on…

You can use the df.index property to get the index of the rows from the Pandas Dataframe. You can also use single and multiple conditions and string matching using the indexing attribute.

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.

You can pass the condition to the index property to select the subset of rows based on the condition and return its index as a series. Then you can use the tolist() method to convert the series of indexes into a list.

Code

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

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

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

Output

    [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

The below code demonstrates how to get the index of rows based on column value with a single condition.

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

The below code demonstrates how to get the index of rows based on column value with multiple conditions.

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.

You can use the isna() method to select the rows with missing values and pass them to the index property.

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.

You can select rows containing a specific string using the == operator.

Code

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

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

Output

    [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.

You can select rows with a partial match string using the str.contains() method.

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

Conclusion

You’ve learned how to get the index of rows based on column values. You’ve also learned how to get the index of the rows using a single condition, multiple conditions and get the index of rows with missing values.

You May Also Like

Leave a Comment