Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
FREEDOM251

Freedom251 › Blog › Data Analysis

How to Find the Index of a Row in a Pandas DataFrame

By Freedom251 Team · · 3 min read

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

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

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

Match on multiple columns

Join conditions with & for AND, | for OR, or ~ to invert a condition. Put each comparison in parentheses:

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.

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

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.

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

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.

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

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:

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.

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.

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

Data AnalysisDataFramepandasPython

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.