14.7 C
London
Tuesday, July 2, 2024
HomePandas in PythonGeneral Functions in PythonHow to Get Row Numbers in a Pandas DataFrame

How to Get Row Numbers in a Pandas DataFrame

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 01, 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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories