PeterL
PeterL

Reputation: 515

Find value of first occurrence in following rows after certain value in row in Pandas

I would like to find the value of the first occurrence after certain value in row.

Test dataframe:

df = pd.DataFrame({'part': ["Toyota", "basic", "upgraded", "Skoda", "basic", "upgraded", "VW", "basic", "upgraded"],
                   'id': ["", 1, 2, "", 4, 5, "", 6, 7]})
       part id
0    Toyota   
1     basic  1
2  upgraded  2
3     Skoda   
4     basic  4
5  upgraded  5
6        VW   
7     basic  6
8  upgraded  7

I am looking for the value in the "upgraded" row that is after "Skoda" - thus 5.

Any tips how to do this in Pandas dataframe?

Thank you.

Upvotes: 1

Views: 158

Answers (1)

mozway
mozway

Reputation: 261015

You can use boolean indexing with masks:

# locate "upgraded" items
m1 = df['part'].eq('upgraded')

# mask values after the first "Skoda"
m2 = df['part'].eq('Skoda').cummax()

# find all "upgraded" that are after first "Skoda"
# and slice first occurrence
df[m1&m2].iloc[0]

output:

part    upgraded
id             5
Name: 5, dtype: object

Upvotes: 1

Related Questions