Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean comparison of two Series objects

Tags:

python

pandas

I have two Series which have a format equal to this:

0    False
1    False
2    False
3    True
4    True
Name: foo, dtype: bool

0    True
1    False
2    False
3    True
4    True
Name: bar, dtype: bool

I want to create a new Series with the resulting boolean comparison from these. Something like this:

result = foo and bar
>>> print result
0    False
1    False
2    False
3    True
4    True
Name: result, dtype: bool

Using the obvious result = foo and bar simply results in the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I looked at those functions, but neither seem to do what I wish.

How can I do an element-to-element boolean comparison of a Series resulting in a new Series?

like image 998
veor Avatar asked Jul 02 '14 15:07

veor


1 Answers

You need to use the bitwise and operator &.

result = foo & bar
like image 111
chrisb Avatar answered Oct 26 '22 08:10

chrisb