0

I am "anding" a binary and non binary column in a pandas DataFrame. To my surprise, this actually gave a result (I was expecting an error). Take a look at the following code:

import pandas as pd
d = {'col1':[1,1,2,2,2], 'col2':[3,4,4,4,3]}
test_df = pd.DataFrame(data = d)
test_df['bool1'] = [True, False, True, True, False]
test_df['bool2'] = [True, False, True, True, True]
test_df['col3'] = [1,3,3,5,5]
test_df['col3'] & test_df['bool1']

I get the following result:

0     True
1    False
2     True
3     True
4    False
dtype: bool

How does pandas evaluate this? col3 is not boolean/binary so I have a hard time determining what drives the decision on whether the combination of both is True or False?

user32882
  • 5,094
  • 5
  • 43
  • 82

1 Answers1

0

Here are casted 0 to False and another integers to Trues:

d = {'col1':[1,1,2,2,2], 'col2':[3,4,4,4,3]}
test_df = pd.DataFrame(data = d)
test_df['bool1'] = [True, False, True, True, False]
test_df['bool2'] = [True, False, True, True, True]
#changed first value to 0
test_df['col3'] = [0,3,3,5,5]

print (test_df['col3'] & test_df['bool1'])
0    False
1    False
2     True
3     True
4    False
dtype: bool

It working like casted by astype:

print (test_df['col3'].astype(bool))
0    False
1     True
2     True
3     True
4     True
Name: col3, dtype: bool

Next reading - Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252