Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

“Index” can mean a row’s index label, its zero-based position, or the labels of rows matching a condition. For rows matching column data, use a boolean condition and select the DataFrame index: df.index[df["name"].eq("Alice")]. That returns all matching labels, not just the first one.

Choose the right meaning of “index”

What you know or need Use What you get
A condition on column values; need matching labels df.index[mask] Index labels for every matching row
A known index label; need its location df.index.get_loc(label) An integer, slice, or boolean mask, depending on index uniqueness and order
A zero-based row position; need its label df.index[position] The label at that position
A zero-based row position; need the row df.iloc[position] The row at that position
A known label; need the row or rows df.loc[label] Row selection by label

In pandas, .loc is primarily label-based, while .iloc is integer-position-based. An integer index label is still a label; it is not automatically a row position. See the pandas indexing and selecting data guide.

Find labels for rows matching a column value

Build a boolean condition from the column, then use it to select the index:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mask = df["name"].eq("Alice")
matching_labels = df.index[mask]

matching_labels contains the index labels for every row whose name is "Alice". If you need the full matching rows rather than their labels, select them with the same condition:

matching_rows = df.loc[mask]

If nothing matches, the selected labels are empty. Decide in your own code whether an empty result is valid or should trigger a separate outcome.

Find rows matching more than one condition

Use & for “and,” | for “or,” and ~ to invert a condition. Put each comparison in parentheses:

mask = (df["name"].eq("Alice")) & (df["city"].eq("Paris"))
matching_labels = df.index[mask]

Here, only rows where both conditions are true are selected. Parentheses matter because Python operator precedence can otherwise change how a compound condition is evaluated.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Locate a known index label

When you already know a label and want its location representation, use get_loc:

location = df.index.get_loc("row_17")

The result is not always one integer. For a unique label, it is an integer; for a repeated label in a monotonic index, it is a slice; and for a repeated label in a non-monotonic index, it is a boolean mask. A missing label raises KeyError. The pandas Index.get_loc reference documents these result forms.

To select rows by a known label instead of getting its location, use df.loc[label]. A repeated label can select more than one row.

Get a row label or row by its position

Positions are zero-based, so position 3 means the fourth row in the DataFrame’s current order:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
label = df.index[3]
row = df.iloc[3]

The first expression gets the index label at that position. The second gets the row itself. An out-of-bounds position passed to .iloc raises IndexError.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle duplicate labels and duplicate row contents separately

Check whether index labels repeat

Repeated index labels can make a label lookup refer to multiple rows. Check uniqueness before assuming a label identifies exactly one row:

df.index.is_unique

Use df.index.duplicated() to identify repeated labels. If a lookup must produce exactly one row, confirm that the index is unique or handle the multiple-result case explicitly. The pandas Index.duplicated reference describes the duplicate-label check.

Find duplicate row contents

Duplicate index labels are not the same as duplicate values in row columns. To mark rows duplicated by selected columns, use DataFrame.duplicated with subset:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
duplicate_mask = df.duplicated(subset=["name", "city"])
duplicate_labels = df.index[duplicate_mask]

The first line returns a boolean Series; the keep setting determines which occurrences are marked. The second line maps those marked rows to their index labels. pandas documents DataFrame.duplicated and its available options.

Check documentation for your pandas version

The official documentation pages cited here identify pandas 3.0.6. API details can differ across releases or index types, so consult the documentation matching the version installed in your project when version-specific behavior matters.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.