0

I have the following sample data frame:

df = pd.DataFrame({
    'seq':[0,1,2,3,4,5,6,7,8,9,10,11],
    'flag1':[np.nan,np.nan,1,1,1, 0,-1,-1,1,1,1,0],
    'flag2':[np.nan,np.nan,np.nan,0, 0,0,-1,-1,0,1, 0,1]
})

I am trying to get the index of the first row where both flag1 and flag2 values are 1. In the above case, it would be 9.

I tried df[df.flag1 == 1.0 & df.flag2 == 1.0].index[0] but it returns me an error. Similarly, df[df.flag1 == 1.0] & df[df.flag2 == 1.0].index[0] does not work either. I tried searching on SO but could not find a solution for my specific need.

2
  • "that returns me an error" . Please include the exact error message in your question. Commented Feb 19, 2020 at 15:12
  • Here is a relevant SO post: stackoverflow.com/questions/17216153/… Commented Feb 19, 2020 at 15:17

1 Answer 1

2

In this expression:

df.flag1 == 1.0 & df.flag2 == 1.0

the & operator has greatest precedence, so it is actually interpreted as:

df.flag1 == (1.0 & df.flag2) == 1.0

which is entirely not what you meant, and in this case produces an error.

Add parentheses to force the evaluation order you want:

(df.flag1 == 1.0) & (df.flag2 == 1.0)

With this change, your initial approach should work fine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.