【问题标题】:AttributeError: 'numpy.bool_' object has no attribute 'isin'AttributeError:“numpy.bool_”对象没有属性“isin”
【发布时间】:2022-07-08 20:25:39
【问题描述】:

我有一个看起来有点像这样的数据框:

cancelled | offer
----------|------
N         | 123 
N         | 456 
y         | 789 

我正在尝试使用 if 语句来标记数据框中是否有任何已取消的报价(即标记为“Y”或“y”的报价)。这是我到目前为止的代码:

if df["cancelled"].any().isin(["Y","y"]):
    print("WARNING - Cancelled offers included!")
else:
     print("OK - No cancelled offers are included.")

但是,当我运行它时,我收到以下错误:

 AttributeError: 'numpy.bool_' object has no attribute 'isin'

显然 isin 函数与我的其余代码不兼容,但在这种情况下,获得所需结果的正确方法是什么?

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    我想你的意思是df["cancelled"].isin(["Y","y"]).any():

    if df["cancelled"].isin(["Y","y"]).any():
        print("WARNING - Cancelled offers included!")
    else:
         print("OK - No cancelled offers are included.")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多