4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Find First Row that Meets Criteria

Pandas: How to Find First Row that Meets Criteria

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...

You can use the following syntax to find the first row in a pandas DataFrame that meets specific criteria:

#get first row where value in 'team' column is equal to 'B'
df[df.team == 'B'].iloc[0]

#get index of first row where value in 'team' column is equal to 'B'
df[df.team == 'B'].index[0]

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', 'C'],
                   'points': [18, 13, 19, 14, 24, 21, 20, 28],
                   'assists': [5, 7, 17, 9, 12, 9, 5, 12]})

#view DataFrame
print(df)

  team  points  assists
0    A      18        5
1    A      13        7
2    A      19       17
3    B      14        9
4    B      24       12
5    C      21        9
6    C      20        5
7    C      28       12

Example 1: Find First Row that Meets One Criteria

We can use the following syntax to find the first row where the value in the team column is equal to ‘B’:

#find first row where team is equal to 'B'
df[df.team == 'B'].iloc[0]

team        B
points     14
assists     9
Name: 3, dtype: object

#find index of first row where team is equal to 'B'
df[df.team == 'B'].index[0]

3

We can see that the first row where the value in the team column is equal to ‘B’ is in index position 3.

Example 2: Find First Row that Meets Multiple Criteria

We can use the following syntax to find the first row where the value in the points column is greater than 15 and the value in the assists column is greater than 10:

#find first row where points > 15 and assists > 10
df[(df.points > 15) & (df.assists > 10)].iloc[0]

team        A
points     19
assists    17
Name: 2, dtype: object

#find index of first row where points > 15 and assists > 10
df[(df.points > 15) & (df.assists > 10)].index[0]

2

We can see that the first row where the value in the points column is greater than 15 and the value in the assists column is greater than 10 is in index position 2.

Example 3: Find First Row that Meets One of Several Criteria

We can use the following syntax to find the first row where the value in the points column is greater than 15 or the value in the assists column is greater than 10:

#find first row where points > 15 or assists > 10
df[(df.points > 15) | (df.assists > 10)].iloc[0]

team        A
points     18
assists     5
Name: 0, dtype: object

#find index of first row where points > 15 or assists > 10
df[(df.points > 15) | (df.assists > 10)].index[0]

0

We can see that the first row where the value in the points column is greater than 15 or the value in the assists column is greater than 10 is in index position 0.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Select Rows without NaN Values in Pandas
How to Select Rows Based on Column Values in Pandas
How to Select Unique Rows in Pandas

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