【问题标题】:How do I deal with this np.where script?我该如何处理这个 np.where 脚本?
【发布时间】:2020-04-24 01:53:22
【问题描述】:

这是我的脚本:

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'

【问题讨论】:

  • 错误信息有什么不清楚的地方?你期望| 做什么?
  • 我做错了什么,我想编写正确的代码。我有一个包含更多固件版本的数据库,我想提取具有正确固件编号(101122、101222、101320、19、01.10、01.03、1000320、11000320)的数字,这就是我添加 OK 和 NOK 的原因
  • 我们不知道您使用 (19) | (01.10) 之类的表达式的意图。

标签: python numpy


【解决方案1】:

你想做什么? np.where 的第一个参数是一个条件表达式,一个布尔值。 |numpy 数组一起使用,logical_or - 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) 可能确实有效,为系列的每个元素生成一个布尔值,具体取决于== 是否为真。

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2021-04-01
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多