veor
veor

Reputation: 1023

Boolean comparison of two Series objects

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?

Upvotes: 11

Views: 14111

Answers (1)

chrisb
chrisb

Reputation: 52236

You need to use the bitwise and operator &.

result = foo & bar

Upvotes: 22

Related Questions