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

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

我正在尝试将以下条件存储在变量中 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]

你的方括号好像放错地方了。那,在评估多个布尔条件时使用括号始终是最佳做法。

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

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

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