When I was working with data in Python, I often needed to locate specific rows in my Pandas DataFrames. Finding the row index is a common task that can be approached in several ways depending on your specific needs.
In this article, I’ll share multiple methods to get the index of a row in a Pandas DataFrame based on my decade of experience working with Python and data analysis.
Let us get started…!
DataFrame Index in Pandas
Before we get into the methods, let’s understand what an index is in Pandas. The index is essentially a label for each row in your DataFrame, similar to how we have row numbers in spreadsheet applications.
By default, Pandas assigns integer indices starting from 0, but you can customize these indices to use any values that make sense for your data.
Methods to Find the Index of a Row in Pandas DataFrame
Let me explain to you the methods to find the index of a row in a Pandas DataFrame.
1 – Use Boolean Indexing
Boolean indexing is one of the most common and flexible ways to find row indices in Python Pandas.
import pandas as pd
# Sample data - US cities population data
data = {
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
'Population': [8336817, 3979576, 2693976, 2320268, 1680992],
'State': ['NY', 'CA', 'IL', 'TX', 'AZ']
}
df = pd.DataFrame(data)
print(df)
# Get indices of rows where the state is 'TX'
texas_indices = df.index[df['State'] == 'TX'].tolist()
print(f"Indices of rows with Texas cities: {texas_indices}")
# Get indices of cities with population over 3 million
large_city_indices = df.index[df['Population'] > 3000000].tolist()
print(f"Indices of large cities: {large_city_indices}")Output:
City Population State
0 New York 8336817 NY
1 Los Angeles 3979576 CA
2 Chicago 2693976 IL
3 Houston 2320268 TX
4 Phoenix 1680992 AZ
Indices of rows with Texas cities: [3]
Indices of large cities: [0, 1]I executed the above example code and added the screenshot below.

In this example, we use boolean conditions to filter the DataFrame and then access the index attribute of the resulting DataFrame.
2 – Use the get_loc() Method
If you’re looking for a specific value in an index, the Python get_loc() method can directly return its position.
# First, let's set the 'City' column as the index
df_indexed = df.set_index('City')
print(df_indexed)
# Find the position of 'Chicago' in the index
chicago_index = df_indexed.index.get_loc('Chicago')
print(f"Position of Chicago in the index: {chicago_index}")Output:
Population State
City
New York 8336817 NY
Los Angeles 3979576 CA
Chicago 2693976 IL
Houston 2320268 TX
Phoenix 1680992 AZ
Position of Chicago in the index: 2I executed the above example code and added the screenshot below.

The get_loc() method is particularly useful when you’ve set a meaningful column as your index and need to find specific values.
3 – Use loc[] with a Condition
Python loc[] accessor can be combined with conditions to find specific rows and their indices.
# Find the index of the row where Phoenix is located
phoenix_row = df.loc[df['City'] == 'Phoenix']
phoenix_index = phoenix_row.index[0]
print(f"Index of Phoenix: {phoenix_index}")
# For multiple matches, get all indices
cities_az_ca = df.loc[df['State'].isin(['AZ', 'CA'])]
indices_az_ca = cities_az_ca.index.tolist()
print(f"Indices of cities in AZ or CA: {indices_az_ca}")Output:
Index of Phoenix: 4
Indices of cities in AZ or CA: [1, 4]I executed the above example code and added the screenshot below.

This method is helpful when you need to filter based on complex conditions before extracting the indices.
Check out Read a CSV to the dictionary using Pandas in Python
4 – Use iloc[] for Position-Based Indexing
If you know the position of a row and need its index label, you can use iloc[].
# Get the index label of the 3rd row (position 2)
third_row_index = df.iloc[2].name
print(f"Index of the third row: {third_row_index}")
# Get indices for a range of positions
range_indices = df.iloc[1:4].index.tolist()
print(f"Indices for positions 1 through 3: {range_indices}")This approach is useful when working with position-based operations and then needing to reference the original indices.
5 – Use idxmax() and idxmin() for Extreme Values
When you need to find the index of the maximum or minimum value in a column, idxmax() and idxmin() are convenient.
# Find the index of the row with the highest population
highest_pop_index = df['Population'].idxmax()
print(f"Index of city with highest population: {highest_pop_index}")
print(f"This is {df.loc[highest_pop_index, 'City']} with {df.loc[highest_pop_index, 'Population']} people")
# Find the index of the row with the lowest population
lowest_pop_index = df['Population'].idxmin()
print(f"Index of city with lowest population: {lowest_pop_index}")
print(f"This is {df.loc[lowest_pop_index, 'City']} with {df.loc[lowest_pop_index, 'Population']} people")These methods are perfect for quickly identifying the indices of rows containing extreme values in your dataset.
Read Convert Pandas Dataframe to Tensor Dataset
6 – Use apply() with a Custom Function
For more complex conditions, you can use apply() with a custom function and then find the indices.
# Define a function to identify cities with names longer than 7 characters
def long_city_name(row):
return len(row['City']) > 7
# Apply the function and get indices
long_name_indices = df.index[df.apply(long_city_name, axis=1)].tolist()
print(f"Indices of cities with names longer than 7 characters: {long_name_indices}")This approach gives you flexibility when the condition for finding indices involves multiple columns or complex logic.
Handle Multiple Matching Rows
Often, your condition might match multiple rows. Here’s how to handle that:
# Add more cities to our DataFrame
more_cities = {
'City': ['Dallas', 'San Antonio', 'San Diego', 'Austin'],
'Population': [1345047, 1434625, 1425976, 961855],
'State': ['TX', 'TX', 'CA', 'TX']
}
df2 = pd.DataFrame(more_cities)
df_combined = pd.concat([df, df2], ignore_index=True)
print(df_combined)
# Get all indices for Texas cities
texas_cities_indices = df_combined.index[df_combined['State'] == 'TX'].tolist()
print(f"Indices of all Texas cities: {texas_cities_indices}")
# Get the names of these cities for reference
texas_cities = df_combined.loc[texas_cities_indices, 'City'].tolist()
print(f"Texas cities: {texas_cities}")When working with real datasets, handling multiple matches is a common scenario, and properly collecting all matching indices is crucial.
Check out Convert a DataFrame to JSON in Python
Work with Custom Indices
Pandas allows you to set custom indices, which can be helpful for more intuitive data access:
# Set City as the index
df_city_indexed = df_combined.set_index('City')
print(df_city_indexed)
# Now you can directly access rows by city name
houston_data = df_city_indexed.loc['Houston']
print(f"Houston data:\n{houston_data}")
# Getting the original numeric position of 'Dallas' in this index
dallas_position = df_city_indexed.index.get_loc('Dallas')
print(f"Position of Dallas in the city-indexed DataFrame: {dallas_position}")Custom indices can make your code more readable and intuitive, especially when working with named entities like cities, products, or customers.
Finding row indices in Pandas is a fundamental skill that helps you navigate, filter, and manipulate your data effectively. The method you choose depends on your specific needs and the structure of your data.
These six methods cover most scenarios you’ll encounter in real-world data analysis. The methods are: using Boolean indexing, get_loc() method, loc[] with a condition, iloc[] for position-based indexing, idxmax(), idxmin() for extreme values, and apply() with a custom function.
Pandas-related tutorials:
- Pandas Dataframe drop() Function in Python
- pd.crosstab Function in Python
- Convert a DataFrame to JSON Array in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.