Search code examples
pythonpandasdataframevalueerror

Why does a boolean if statement using pandas create an error?


How do I format this to be able to test with an if statement?

result = dfrow.str.contains('Animation')
x = 0
print(result)
if result == False:
    x = 1
if result == True:
    x = 2

The print function outputs:

1    False

Name: genres, dtype: bool

The error given:

----> 4 if result == False:

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

Solution

  • It seems you will be using a 1 row dataframe for this scenario. Then you can use

    dfrow.str.contains('Animation').iloc[0]
     instead of 
    dfrow.str.contains('Animation')