【问题标题】:Why does a boolean if statement using pandas create an error? [closed]为什么使用 pandas 的布尔 if 语句会产生错误? [关闭]
【发布时间】:2020-02-20 05:15:26
【问题描述】:

如何设置格式以便能够使用 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().

【问题讨论】:

标签: python pandas dataframe valueerror


【解决方案1】:

看来您将在这种情况下使用 1 行数据框。然后就可以使用了

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

【讨论】:

  • 为什么他只想使用第一个值?
  • @ansev 即使我们输出多个值,接下来的步骤也会中断,因为 op 将其视为单个 True 或 False 变量
猜你喜欢
  • 2019-11-15
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多