2

In the code:

df = pd.DataFrame([[False, 0], [False, 1], [False, 2], [False, 2], [False, 3], [False, 4], [False, 5]], 
        columns=['cond1', 'cond2'])
df['test'] = (~df['cond1']) & (df['cond2'])

I'm expecting to get in the test column the value True in all rows except the first, as the truthy value of any number but 0 is True. However, I'm getting False where the value in cond2 is 2 or 4. Why?

The output of the above code:

   cond1 cond2 test

0  False 0     False 

1  False 1     True 

2  False 2     False 

3  False 2     False 

4  False 3     True 

5  False 4     False 

6  False 5     True 
Aryerez
  • 3,417
  • 2
  • 9
  • 17

1 Answers1

1

When you're doing "&" operation with pandas column, python can't interpret the other numbers. It's doing set operation instead of boolean operation. Therefore, you need to help it by converting to bool the second column:

df = pd.DataFrame([[False, 0], [False, 1], [False, 2], [False, 2], [False, 3], [False, 4], [False, 5]], 
        
columns=['cond1', 'cond2'])

df['test'] = (~df['cond1']) & ((df['cond2'].astype(bool)))
Ewelina Luczak
  • 379
  • 4
  • 13