Recommended Free Tools
“Index” can mean a row’s index label, its zero-based position, or the labels of rows whose column values match a condition. For rows matching data, use a boolean mask and select from df.index; for a known position, use df.index[position]; for a known label, use df.index.get_loc(label).
Find index labels for rows matching a value
Build a condition from the relevant column, then apply it to the DataFrame’s index. This returns every matching label, rather than assuming there is only one match.
As an Amazon Associate I earn from qualifying purchases.
mask = df["name"].eq("Alice")
matching_labels = df.index[mask]
If you need the matching rows rather than their labels, use df.loc[mask]. If no rows match, matching_labels is empty; decide whether your code should accept that or handle it explicitly.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsMatch on multiple columns
Join conditions with & for AND, | for OR, or ~ to invert a condition. Put each comparison in parentheses:
#1 Best Overall
mask = (df["name"].eq("Alice")) & (df["city"].eq("Paris"))
matching_labels = df.index[mask]
The boolean mask acts as a row selector and must align with the DataFrame’s rows. Parentheses matter because Python’s operator precedence can otherwise change how a combined expression is evaluated. See the pandas indexing and selecting data guide.
Get a row’s label or row by position
A position is the row’s zero-based place in the DataFrame’s current order. Position 3 is the fourth row. Use df.index[position] to get that row’s label, or df.iloc[position] to retrieve the row itself.
label = df.index[3] # label at zero-based position 3
row = df.iloc[3] # row at zero-based position 3
Positions are not necessarily the same as index labels. For instance, the fourth row could have a date, a string, or an integer label other than 3. An out-of-bounds integer position passed to .iloc raises IndexError.
Locate a known index label
If you already know the label and want its location information, call get_loc on the index:
location = df.index.get_loc("row_17")
For a unique label, the result is an integer. With repeated labels, the result can instead be a slice (for a repeated label in a monotonic index) or a boolean mask (for a repeated label in a non-monotonic index). It is therefore unsafe to assume the result is always one integer unless the index is unique. A missing label raises KeyError. The pandas Index.get_loc reference describes the result as an integer location, slice, or boolean mask for the requested label.
Choose between labels, positions, and row selection
| Your input or goal | Use | Result |
|---|---|---|
| Column values that identify one or more rows | df.index[mask] |
Index labels for all matching rows |
| Column values that identify rows to retrieve | df.loc[mask] |
Matching rows |
| A known index label | df.index.get_loc(label) |
Location information; may be an integer, slice, or mask |
| A zero-based row position | df.index[position] |
Index label at that position |
| A zero-based row position and the row itself | df.iloc[position] |
Row at that position |
| A known label and the row or rows at that label | df.loc[label] |
Row selection by label |
.loc is primarily label-based, though it also accepts a boolean array; .iloc is integer-position-based. The distinction and error behavior are documented in the pandas indexing guide.
Rank #4
Account for repeated labels and duplicate rows
Repeated index labels and duplicate row contents are different cases. Check whether labels are unique with df.index.is_unique; use df.index.duplicated() to identify repeated labels. When labels repeat, label-based selection or a lookup can correspond to multiple rows.
To identify duplicate row contents based on selected columns, use df.duplicated. Its keep argument controls which occurrences are marked. The result is a boolean Series, not index locations by itself; apply it to the index when you want the labels:
Best Value
duplicate_mask = df.duplicated(subset=["name", "city"], keep="first")
duplicate_labels = df.index[duplicate_mask]
See the pandas documentation for Index.duplicated and DataFrame.duplicated.
Version considerations
The pandas documentation consulted identifies version 3.0.6 on its user guide and API pages. The methods above describe the documented API semantics; if your project uses an older pandas release or a custom index type, consult the documentation for that installed version where behavior details matter.
Quick Recap
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.

