如何处理这个 np.where 脚本?



这是我的脚本:

df_smr5['FWCorrect'] = np.where((df_smr5['Firmware E'] == (101122), (101222) | (101320), (19) | (01.10), (01.03) | (1000320), (11000320)), 'OK', 'NOK')

我收到此错误:

TypeError
Traceback (most recent call last) 
<ipython-input-5-52adab99696c> in <module>()
----> 1 df_smr5['FWCorrect'] = np.where((df_smr5['Firmware E'] == (101122), (101222) | (101320), (19) | (01.10), (01.03) | (1000320), (11000320)), 'OK', 'NOK')
TypeError: unsupported operand type(s) for |: 'int' and 'float'

你想做什么? 要np.where的第一个参数是一个条件表达式,一个布尔值。|logical_or意义上与numpy数组一起使用 -or应用于布尔数组的元素。

但是您正在将其与数字一起使用 - 整数和浮点数。(19)是一个数字,而不是一个元组。

In [61]: (19) | (01.10)                                                         
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-61-212c5dce76e6> in <module>
----> 1 (19) | (01.10)
TypeError: unsupported operand type(s) for |: 'int' and 'float'
In [62]: (101222) | (101320)                                                    
Out[62]: 101358

如果您打算使用元组:

In [63]: (101222,) | (101320,)                                                  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-63-2b5f4e4af1f0> in <module>
----> 1 (101222,) | (101320,)
TypeError: unsupported operand type(s) for |: 'tuple' and 'tuple'

df_smr5['Firmware E'] == (101122)可能确实有效,为系列的每个元素生成一个布尔值,具体取决于==是否为真。