Get the Index of Rows With Certain Column Value in Pandas | Towards Data Science

Sometimes, we may need to retrieve the index of DataFrame rows whose column matches a specific value. In today’s short guide we will explore how to do so in pandas.
Specifically, we will showcase how to get the indices using
the index property of pandas DataFrames
and the NumPy’s where() method
Additionally, we will also discuss how you can then use the retrieved indices in order to perform common tasks.
First, let’s create an example DataFrame that we’ll reference throughout this article in order to demonstrate a few concepts around the topic of interest.
import pandas as pddf = pd.DataFrame(    [        (1, 100, 10.5, True),        (2, 500, 25.6, False),         (3, 150, 12.3, False),        (4, 100, 76.1, True),        (5, 220, 32.1, True),        (6, 100, 10.0, True),    ],    columns=['colA', 'colB', 'colC', 'colD'])print(df)   colA  colB  colC   colD0     1   100  10.5   True1     2   500  25.6  False2     3   150  12.3  False3     4   100  76.1   True4     5   220  32.1   True5     6   100  10.0   True
The first option you have when it comes to accessing the index is pandas.DataFrame.index property returns the index (i.e. the row labels) of a pandas DataFrame.
For example, let’s assume we want to retrieve the indices of all the rows whose column value in colD is True. The following will do the trick:
idx = df.index[df['colD']]print(idx)Int64Index([0, 3, 4, 5], dtype='int64')
Now if you want to store the indices into a list then you can simply use tolist() method:
indices = df.index[df['colD']].tolist()print(indices)[0, 3, 4, 5]
Alternatively, you can use NumPy’s where() method that returns an ndarray containing the elements chosen based on the specified condition.
For example, in order to get the index of all the rows whose column values in colB is equal to 100 then the following expression will do the trick:
import numpy as npindices = np.where(df['colB'] == 100)print(indices)(array([0, 3, 5]),)
Now that we know how to get the index of specific rows whose columns match certain values, you may want to somehow utilise these indices.
For example, in order to select rows from pandas DataFrame using the retrieved indices, you may use loc property along with the Index:
idx = df.index[df['colD']]df_colD_is_true = df.loc[idx]print(df_colD_is_true)   colA  colB  colC  colD0     1   100  10.5  True3     4   100  76.1  True4     5   220  32.1  True5     6   100  10.0  True
Alternatively, if you have some indices stored in lists and you want to use them directly in order to fetch the corresponding rows you can instead use iloc property:
indices = df.index[df['colD']].tolist()df_colD_is_true = df.iloc[indices]print(df_colD_is_true)   colA  colB  colC  colD0     1   100  10.5  True3     4   100  76.1  True4     5   220  32.1  True5     6   100  10.0  True
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.
Additionally, we explored how to use the retrieved indices in order to fetch the actual rows from a pandas DataFrame.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
gmyrianthous.medium.com
You may also like