HOWTO · Pandas

How to Get Index of Rows Whose Column Matches Specific Value in Pandas

Filter Pandas rows by numeric or string conditions and retrieve their index labels.

On this page

To retrieve rows that match a condition, create a Boolean mask and pass it to DataFrame.loc. To retrieve only their labels, apply the same mask to DataFrame.index; call tolist() if you need a regular Python list. Index labels identify rows and need not be consecutive integer positions. Reusing one mask keeps the row selection and label selection aligned, even when a DataFrame has custom labels. Choose DataFrame.loc when you need the matching records for further work, and use the index expression when the labels themselves are the desired result.

Get Rows Matching Numeric Conditions in Pandas

This example selects rows where column B equals 19, then combines that condition with an OR condition on column C. DataFrame.loc returns the matching rows, while DataFrame.index returns their labels. Parentheses around each comparison make the combined Boolean condition explicit. In a real DataFrame, replace the sample column names and values with those in your data; keep each comparison parenthesized when combining masks with | or & so Python applies the intended operation to each Boolean Series.

import numpy as np
import pandas as pd

np.random.seed(0)
df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

matches_b = df["B"] == 19
print(df.loc[matches_b])
print(df.index[matches_b].tolist())

matches_b_or_c = (df["B"] == 19) | (df["C"] == 19)
print(df.loc[matches_b_or_c])
print(df.index[matches_b_or_c].tolist())
    A   B  C   D
6   1  19  4  18
9  12  19  3   1
[6, 9]
     A   B   C   D
6    1  19   4  18
9   12  19   3   1
14  12  15  19   1
[6, 9, 14]

The first mask selects the rows whose B value is 19; its labels are [6, 9]. The second mask uses OR (|), so a row is selected when either B or C is 19; its labels are [6, 9, 14]. Use & between parenthesized conditions when both must be true. These are index labels, not row positions, so labels can remain meaningful even if the DataFrame was filtered or loaded with a custom index. DataFrame.index is an attribute, rather than a method, and tolist() converts the selected labels to a Python list without relying on a particular pandas index class. If the original labels are not unique, the result can contain repeated labels because each matching row contributes its index label.

Get Row Indices for String Matches in Pandas

String filters use the same pattern: compare a column to a full value for exact equality, or use str.contains to find a substring. Exact equality is useful when the entire cell value must match; substring matching is useful when the value may include other text. The example below retrieves both matching rows and their labels. The substring condition uses lowercase rex, which appears in Tyrannosaurus rex with the same case.

import pandas as pd

df = pd.DataFrame(
    {
        "Name": ["blue", "delta", "echo", "charlie", "alpha"],
        "Type": ["Raptors", "Raptors", "Raptors", "Raptors", "Tyrannosaurus rex"],
    }
)

exact_name = df["Name"] == "blue"
print(df.index[exact_name].tolist())
print(df.loc[exact_name])

name_contains_ha = df["Name"].str.contains("ha")
print(df.index[name_contains_ha].tolist())
print(df.loc[name_contains_ha])

name_contains_ha_and_type_contains_rex = name_contains_ha & df["Type"].str.contains("rex")
print(df.loc[name_contains_ha_and_type_contains_rex])
[0]
   Name     Type
0  blue  Raptors
[3, 4]
      Name               Type
3  charlie            Raptors
4    alpha  Tyrannosaurus rex
    Name               Type
4  alpha  Tyrannosaurus rex

Exact equality finds blue and returns its label. The ha substring matches charlie and alpha; combining it with the rex condition across Type selects the alpha row, whose label is [4]. str.contains performs case-sensitive matching by default, so match the capitalization in the data or set its case option deliberately. Combine conditions with & when both tests must match and with | when either may match; parenthesize each comparison. As with numeric filters, pass the mask to DataFrame.loc for rows and to DataFrame.index for labels. The exact-match expression can be adapted to another column by changing the column name and comparison value, while the substring form keeps the same structure with a different search string.