In Python:
In [1]: True+True
Out[1]: 2
So after the following set-up:
import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False])
What I want is to find the element-wise sum of ser1
and ser2
, with the booleans treated as integers for addition as in the Python example.
But Pandas treats the addition as an element-wise "or" operator, and gives the following (undesired) output:
In [5]: ser1+ser2
*/lib/python2.7/site-packages/pandas/computation/expressions.py:184: UserWarning: evaluating in Python space because the '+' operator is not supported by numexpr for the bool dtype, use '|' instead
unsupported[op_str]))
Out[5]:
0 True
1 True
2 True
3 False
dtype: bool
I know I get can get my desired output using astype(int)
on either series:
In [6]: ser1.astype(int) + ser2
Out[6]:
0 2
1 1
2 1
3 0
dtype: int64
Is there another (more "pandonic") way to get the [2,1,1,0] series? Is there a good explanation for why simple Series addition doesn't work here?
Instead of +
use &
import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False])
print(ser1 & ser2)
>> 0 True
>> 1 False
>> 2 False
>> 3 False
>> dtype: bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With