How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?

How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?

To retrieve the index of rows in a Pandas DataFrame that contain a specific value in a particular column, one can use the “loc” method. This method allows for selecting rows based on certain conditions, such as a column’s value. By specifying the desired column and value, the “loc” method will return the index of all rows that meet the given criteria. This can be useful for identifying and manipulating specific rows within a DataFrame.

Pandas: Get Index of Rows Whose Column Matches Value


You can use the following syntax to get the index of rows in a pandas DataFrame whose column matches specific values:

df.index[df['column_name']==value].tolist()

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'D'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

team	points	rebounds
0	A	5	11
1	A	7	8
2	A	7	10
3	B	9	6
4	B	12	6
5	C	9	5
6	C	9	9
7	D	4	12

Example 1: Get Index of Rows Whose Column Matches Value

The following code shows how to get the index of the rows where one column is equal to a certain value:

#get index of rows where 'points' column is equal to 7
df.index[df['points']==7].tolist()

[1, 2]

This tells us that the rows with index values 1 and 2 have the value ‘7’ in the points column.

Note that we can also use the less than and greater than operators to find the index of the rows where one column is less than or greater than a certain value:

#get index of rows where 'points' column is greater than 7
df.index[df['points']>7].tolist()

[3, 4, 5, 6]

This tells us that the rows with index values 3, 4, 5, and 6 have a value greater than ‘7’ in the points column.

Example 2: Get Index of Rows Whose Column Matches String

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]

This tells us that the rows with index values 3 and 4 have the value ‘B’ in the team column.

Example 3: Get Index of Rows with Multiple Conditions

The following code shows how to get the index of the rows where the values in multiple columns match certain conditions:

#get index of rows where 'points' is equal to 7 or 12
df.index[(df['points']==7) | (df['points']==12)].tolist()

[1, 2, 4]

#get index of rows where 'points' is equal to 9 and 'team' is equal to 'B'
df.index[(df['points']==9) & (df['team']=='B')].tolist()

[3]

Cite this article

stats writer (2024). How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-get-the-index-of-rows-in-a-pandas-dataframe-whose-column-matches-a-specific-value/

stats writer. "How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-get-the-index-of-rows-in-a-pandas-dataframe-whose-column-matches-a-specific-value/.

stats writer. "How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-get-the-index-of-rows-in-a-pandas-dataframe-whose-column-matches-a-specific-value/.

stats writer (2024) 'How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-get-the-index-of-rows-in-a-pandas-dataframe-whose-column-matches-a-specific-value/.

[1] stats writer, "How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I get the index of rows in a Pandas DataFrame whose column matches a specific value?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top