Often you may want to get the row numbers in a pandas DataFrame that contain a certain value.
Fortunately this is easy to do using the .index function.
This tutorial shows several examples of how to use this function in practice.
Example 1: Get Row Numbers that Match a Certain Value
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'team': ['Mavs', 'Mavs', 'Spurs', 'Celtics', 'Warriors']}) #view DataFrame print(df) points assists team 0 25 5 Mavs 1 12 7 Mavs 2 15 7 Spurs 3 14 9 Celtics 4 19 12 Warriors
We can use the following syntax to get the row numbers where ‘team’ is equal to Mavs:
#get row numbers where 'team' is equal to Mavs df[df['team'] == 'Mavs'].index Int64Index([0, 1], dtype='int64')
We can see that the team name is equal to ‘Mavs’ at rows indices 0 and 1.
We can also get the row numbers where the team name is in a certain list of team names:
#get row numbers where 'team' is equal to Mavs or Spurs filter_list = ['Mavs', 'Spurs'] #return only rows where team is in the list of team names df[df.team.isin(filter_list)].index Int64Index([0, 1, 2], dtype='int64')
We can see that the team name is equal to ‘Mavs’ or ‘Spurs’ at rows indices 0, 1, and 2.
Example 2: Get a Single Row Number
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'team': ['Mavs', 'Mavs', 'Spurs', 'Celtics', 'Warriors']})
If you know that only one row matches a certain value, you can retrieve that single row number using the following syntax:
#get the row number where team is equal to Celtics df[df['team'] == 'Celtics'].index[0] 3
We can see that team is equal to ‘Celtics’ at row index number 3.
Example 3: Get Sum of Row Numbers
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19], 'assists': [5, 7, 7, 9, 12], 'team': ['Mavs', 'Mavs', 'Spurs', 'Celtics', 'Warriors']})
If you want to know the total number of rows where a column is equal to a certain value, you can use the following syntax:
#find total number of rows where team is equal to Mavs len(df[df['team'] == 'Celtics'].index) 2
We can see that team is equal to ‘Mavs’ in a total of 2 rows.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
How to Find Unique Values in Multiple Columns in Pandas
How to Filter a Pandas DataFrame on Multiple Conditions
How to Count Missing Values in a Pandas DataFrame