dtracers
dtracers

Reputation: 1658

Search in Pandas for first instance of condition

I have a panda table where I am trying to look backwards from a certain row for the index of a condition.

Index    column1    column2
0         True       False
1         True       False
2         False      False
3         True       False
4         False      False
5         False      False
6         False      True
7         False      True

So given the above table. I know the index of column2 and am using that so I have 6 now I want to look back and then find the first instance of a different column changing (column1) In this case it would return 3

Is there a good efficient way to do this in pandas?

Upvotes: 0

Views: 59

Answers (1)

BENY
BENY

Reputation: 323316

Try using shift with idxmax

df.column1.ne(df.column1.shift().bfill()).idxmax()+1
Out[86]: 3

Upvotes: 3

Related Questions