df.iloc[i] returns the ith row of df. i does not refer to the index label, i is a 0-based index.

In contrast, the attribute index returns actual index labels, not numeric row-indices:

df.index[df['BoolCol'] == True].tolist()

or equivalently,

df.index[df['BoolCol']].tolist()

You can see the difference quite clearly by playing with a DataFrame with a non-default index that does not equal to the row's numerical position:

df = pd.DataFrame({'BoolCol': [True, False, False, True, True]},
       index=[10,20,30,40,50])

In [53]: df
Out[53]: 
   BoolCol
10    True
20   False
30   False
40    True
50    True

[5 rows x 1 columns]

In [54]: df.index[df['BoolCol']].tolist()
Out[54]: [10, 40, 50]

If you want to use the index,

In [56]: idx = df.index[df['BoolCol']]

In [57]: idx
Out[57]: Int64Index([10, 40, 50], dtype='int64')

then you can select the rows using loc instead of iloc:

In [58]: df.loc[idx]
Out[58]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

Note that loc can also accept boolean arrays:

In [55]: df.loc[df['BoolCol']]
Out[55]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

If you have a boolean array, mask, and need ordinal index values, you can compute them using np.flatnonzero:

In [110]: np.flatnonzero(df['BoolCol'])
Out[112]: array([0, 3, 4])

Use df.iloc to select rows by ordinal index:

In [113]: df.iloc[np.flatnonzero(df['BoolCol'])]
Out[113]: 
   BoolCol
10    True
40    True
50    True
Answer from unutbu on Stack Overflow
Top answer
1 of 8
817

df.iloc[i] returns the ith row of df. i does not refer to the index label, i is a 0-based index.

In contrast, the attribute index returns actual index labels, not numeric row-indices:

df.index[df['BoolCol'] == True].tolist()

or equivalently,

df.index[df['BoolCol']].tolist()

You can see the difference quite clearly by playing with a DataFrame with a non-default index that does not equal to the row's numerical position:

df = pd.DataFrame({'BoolCol': [True, False, False, True, True]},
       index=[10,20,30,40,50])

In [53]: df
Out[53]: 
   BoolCol
10    True
20   False
30   False
40    True
50    True

[5 rows x 1 columns]

In [54]: df.index[df['BoolCol']].tolist()
Out[54]: [10, 40, 50]

If you want to use the index,

In [56]: idx = df.index[df['BoolCol']]

In [57]: idx
Out[57]: Int64Index([10, 40, 50], dtype='int64')

then you can select the rows using loc instead of iloc:

In [58]: df.loc[idx]
Out[58]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

Note that loc can also accept boolean arrays:

In [55]: df.loc[df['BoolCol']]
Out[55]: 
   BoolCol
10    True
40    True
50    True

[3 rows x 1 columns]

If you have a boolean array, mask, and need ordinal index values, you can compute them using np.flatnonzero:

In [110]: np.flatnonzero(df['BoolCol'])
Out[112]: array([0, 3, 4])

Use df.iloc to select rows by ordinal index:

In [113]: df.iloc[np.flatnonzero(df['BoolCol'])]
Out[113]: 
   BoolCol
10    True
40    True
50    True
2 of 8
52

Can be done using numpy where() function:

import pandas as pd
import numpy as np

In [716]: df = pd.DataFrame({"gene_name": ['SLC45A1', 'NECAP2', 'CLIC4', 'ADC', 'AGBL4'] , "BoolCol": [False, True, False, True, True] },
       index=list("abcde"))

In [717]: df
Out[717]: 
  BoolCol gene_name
a   False   SLC45A1
b    True    NECAP2
c   False     CLIC4
d    True       ADC
e    True     AGBL4

In [718]: np.where(df["BoolCol"] == True)
Out[718]: (array([1, 3, 4]),)

In [719]: select_indices = list(np.where(df["BoolCol"] == True)[0])

In [720]: df.iloc[select_indices]
Out[720]: 
  BoolCol gene_name
b    True    NECAP2
d    True       ADC
e    True     AGBL4

Though you don't always need index for a match, but incase if you need:

In [796]: df.iloc[select_indices].index
Out[796]: Index([u'b', u'd', u'e'], dtype='object')

In [797]: df.iloc[select_indices].index.tolist()
Out[797]: ['b', 'd', 'e']
🌐
Python Guides
pythonguides.com › get-index-pandas-python
Find Index Of Value In Pandas Python
May 22, 2025 - Python .get_loc() method is perfect when you’re working with index objects and need to find the position of a specific value. This method is particularly useful when you know the value exists exactly once in the index: import pandas as pd # Creating a DataFrame with states as index state_data ...
🌐
Statology
statology.org › home › pandas: get index of rows whose column matches value
Pandas: Get Index of Rows Whose Column Matches Value
July 16, 2021 - This tells us that the rows with index values 3, 4, 5, and 6 have a value greater than ‘7’ in the points column. The following code shows how to get the index of the rows where one column is equal to a certain string: #get index of rows where 'team' column is equal to 'B' df.index[df['team']=='B'].tolist() [3, 4]
🌐
Delft Stack
delftstack.com › home › howto › python pandas › pandas get index of row
How to Get Index of Rows Whose Column Matches Specific Value in Pandas | Delft Stack
February 2, 2024 - The reason why we put tolist() behind the index() method is to convert the Index to the list; otherwise, the result is of Int64Index data type. ... Retrieving just the indices can be done based on multiple conditions too.
🌐
w3resource
w3resource.com › python-exercises › pandas › index › pandas-indexing-exercise-20.php
Pandas: Find index of rows where specified column matches certain value - w3resource
import pandas as pd df = pd.DataFrame({ 'school_code': ['s001','s002','s003','s001','s002','s004'], 'class': ['V', 'V', 'VI', 'VI', 'V', 'VI'], 'name': ['Alberto Franco','Gino Mcneill','Ryan Parkes', 'Eesha Hinton', 'Gino Mcneill', 'David Parkes'], 'date_of_birth': ['15/05/2002','17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'], 'weight': [35, 32, 33, 30, 31, 32]}, index = [1, 2, 3, 4, 5, 6]) print("Original DataFrame with single index:") print(df) print("\nIndex of rows where specified column matches certain value:") print(df.index[df['school_code']=='s001'].tolist())
🌐
Saturn Cloud
saturncloud.io › blog › how-to-find-the-index-of-a-value-anywhere-in-a-pandas-dataframe
How to Find the Index of a Value Anywhere in a Pandas DataFrame | Saturn Cloud Blog
May 1, 2026 - Cons: Requires familiarity with NumPy syntax, less readable than Pandas methods for some users. In conclusion, finding the index of a value anywhere in a Pandas DataFrame can be a time-consuming task if done manually. Thankfully, Pandas provides some easy and efficient ways to accomplish this task using the DataFrame.isin(), DataFrame.loc[] and numpy.where() methods.
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.values.html
pandas.Index.values — pandas 3.0.5 documentation - PyData |
For pandas.Index: >>> idx = pd.Index([1, 2, 3]) >>> idx Index([1, 2, 3], dtype='int64') >>> idx.values array([1, 2, 3]) For pandas.IntervalIndex: >>> idx = pd.interval_range(start=0, end=5) >>> idx.values <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] On this page
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas get index from dataframe
Pandas Get Index from DataFrame - Spark By {Examples}
November 6, 2024 - How to get an index from Pandas DataFrame? DataFrame.index property is used to get the index from the DataFrame. Pandas Index is an immutable sequence
🌐
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.6 documentation
In this section, we will focus on the final point: namely, how to slice, dice, and generally get and set subsets of pandas objects. The primary focus will be on Series and DataFrame as they have received more development attention in this area. ... The Python and NumPy indexing operators [] and attribute operator .
🌐
Towards Data Science
towardsdatascience.com › home › latest › getting the index of rows with certain column value in pandas
Getting the Index of Rows With Certain Column Value in Pandas | Towards Data Science
January 20, 2025 - In today's short guide we discussed how to retrieve the index of rows whose column matches a specified value. Specifically, we showcased how to get the indices using the index property of pandas DataFrames as well as the where() method of NumPy library.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.index.html
pandas.DataFrame.index — pandas 3.0.5 documentation
In this example, we create a DataFrame with 3 rows and 3 columns, including Name, Age, and Location information. We set the index labels to be the integers 10, 20, and 30. We then access the index attribute of the DataFrame, which returns an Index object containing the index labels.
🌐
Python Examples
pythonexamples.org › get-index-of-pandas-dataframe
How to Get Index of Pandas DataFrame?
We can print the elements of Index object using a for loop as shown in the following. import pandas as pd df = pd.DataFrame( [[88, 72, 67], [23, 78, 62], [55, 54, 76]], columns=['a', 'b', 'c']) index = df.index for i in index: print(i)
🌐
Linux Hint
linuxhint.com › pandas-get-index-values
Linux Hint – Linux Hint
July 29, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.get_indexer.html
pandas.Index.get_indexer — pandas 3.0.5 documentation
Returns indexer and masks for new index given the current index. ... Returns -1 for unmatched values, for further explanation see the example below. ... Notice that the return value is an array of locations in index and x is marked by -1, as it is not in index.