【问题标题】:TypeError: unsupported operand type(s) for |: 'str' and 'bool with pandasTypeError:不支持的操作数类型|:'str'和'bool with pandas
【发布时间】:2021-03-23 23:02:57
【问题描述】:

我正在尝试将以下条件存储在变量 excel_filtered excel_filtered = excel[excel['prowess_compustat_h1b'] == 1] | excel['compustat_h1b'] == 1

但我收到以下错误

TypeError: unsupported operand type(s) for |: 'str' and 'bool'

编辑

这是一个简单的语法错误,最后缺少一个括号。正确答案如下:

excel_filtered = excel[excel['prowess_compustat_h1b'] == 1] | excel['compustat_h1b'] == 1]

【问题讨论】:

    标签: pandas boolean conditional-statements


    【解决方案1】:

    似乎你在错误的地方放了一个方括号。在评估多个布尔标准时,最好使用括号。

    excel_filtered = excel[(excel['prowess_compustat_h1b'] == 1) | (excel['compustat_h1b'] == 1)]
    

    同样对于这些类型的操作,我发现query方法可以增加可读性:

    excel_filtered = excel.query("(prowess_compustat_h1b == 1) | (compustat_h1b == 1)"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2013-02-20
      • 2018-12-06
      • 1970-01-01
      • 2018-03-20
      • 2019-01-27
      相关资源
      最近更新 更多