为什么使用 pandas 的布尔 if 语句会产生错误?
Why does a boolean if statement using pandas create an error?
如何格式化它以便能够使用 if 语句进行测试?
result = dfrow.str.contains('Animation')
x = 0
print(result)
if result == False:
x = 1
if result == True:
x = 2
打印函数输出:
1 False
Name: genres, dtype: bool
给出的错误:
----> 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().
您似乎将在这种情况下使用 1 行数据框。然后你可以使用
dfrow.str.contains('Animation').iloc[0]
instead of
dfrow.str.contains('Animation')
如何格式化它以便能够使用 if 语句进行测试?
result = dfrow.str.contains('Animation')
x = 0
print(result)
if result == False:
x = 1
if result == True:
x = 2
打印函数输出:
1 False
Name: genres, dtype: bool
给出的错误:
----> 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().
您似乎将在这种情况下使用 1 行数据框。然后你可以使用
dfrow.str.contains('Animation').iloc[0]
instead of
dfrow.str.contains('Animation')