How can I find the first row in a Pandas DataFrame that meets a specific criteria?

How can I find the first row in a Pandas DataFrame that meets a specific criteria?

To find the first row in a Pandas DataFrame that meets a specific criteria, you can use the .loc[] method along with a conditional statement to filter the DataFrame. This will return the first row that satisfies the given condition. Alternatively, you can also use the .iloc[] method to retrieve the first row based on its index. Both of these methods allow for efficient and accurate searching within a DataFrame.

Pandas: Find First Row that Meets Criteria


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.

Cite this article

stats writer (2024). How can I find the first row in a Pandas DataFrame that meets a specific criteria?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-first-row-in-a-pandas-dataframe-that-meets-a-specific-criteria/

stats writer. "How can I find the first row in a Pandas DataFrame that meets a specific criteria?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-first-row-in-a-pandas-dataframe-that-meets-a-specific-criteria/.

stats writer. "How can I find the first row in a Pandas DataFrame that meets a specific criteria?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-first-row-in-a-pandas-dataframe-that-meets-a-specific-criteria/.

stats writer (2024) 'How can I find the first row in a Pandas DataFrame that meets a specific criteria?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-first-row-in-a-pandas-dataframe-that-meets-a-specific-criteria/.

[1] stats writer, "How can I find the first row in a Pandas DataFrame that meets a specific criteria?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I find the first row in a Pandas DataFrame that meets a specific criteria?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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