Let’s reduce it to a simpler example. By doing for instance the following comparison:
3 == pd.Series([3,2,4,1])
0 True
1 False
2 False
3 False
dtype: bool
The result you get is a Series
of booleans, equal in size to the pd.Series
in the right hand side of the expression. So really what’s happening here is that the integer is being broadcast across the series, and then they are compared. So when you do:
if 3 == pd.Series([3,2,4,1]):
pass
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
You get an error. The problem here is that you are comparing a pd.Series
with a value, so you’ll have multiple True
and multiple False
values, as in the case above. This of course is ambiguous, since the condition is neither True
or False
.
So you need to further aggregate the result so that a single boolean value results from the operation. For that you’ll have to use either any
or all
depending on whether you want at least one (any
) or all
values to satisfy the condition.
(3 == pd.Series([3,2,4,1])).all()
# False
or
(3 == pd.Series([3,2,4,1])).any()
# True