Tag Archives: Pandas ValueError

Pandas ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.an

When performing data comparison, pandas reports an error:

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

The selected truth value is not clear, that is, the given value and the comparison value are of different types.

It should be a problem caused by comparing and matching a value with multiple values or values in a list

You can use one of its recommended methods before comparing

1)a.empty

if(a.empty):

    print("!!")  

Judge whether a is empty

2)a.item()

a. Item (I) represents the ith node

3)a.any()

if(a.any() in [1,2,3,4]):

print("!!") 

Judge whether any value in a is in [1,2,3,4]

4)a.all()

if(a.all() in [1,2,3,4]):
    print("!!") 

Judge whether all the values in a are in [1,2,3,4]