【问题标题】:Numpy TypeError: ufunc 'bitwise_and' not supported for the input types,Numpy TypeError:输入类型不支持ufunc'bitwise_and',
【发布时间】:2020-06-24 05:06:30
【问题描述】:

我有以下数据框和列表值

import pandas as pd
import numpy as np
df_merge = pd.DataFrame({'column1': ['a', 'c', 'e'],
               'column2': ['b', 'd', 'f'],
               'column3': [0.5, 0.6, .04],
               'column4': [0.7, 0.8, 0.9]
               })

bb = ['b','h']
dd = ['d', 'I']
ff = ['f', 'l']

我正在尝试使用 np.where 和 np.select 来代替 IF FUNCTION:

condition = [((df_merge['column1'] == 'a') & (df_merge['column2'] == df_merge['column2'].isin(bb))),((df_merge['column1'] == 'c') & (df_merge['column2'] == df_merge['column2'].isin(dd))), ((df_merge['column1'] == 'e') & (df_merge['column2'] == df_merge['column2'].
isin(ff)))]

choices1 = [((np.where(df_merge['column3'] >= 1, 'should not have, ','correct')) & (np.where(df_merge['column4'] >= 0.45, 'should not have, ','correct')))]

df_merge['Reason'] = np.select(condition, choices1, default='correct')

但是,当我尝试运行choices1 的代码行时,我收到以下错误:

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我不确定我们是否可以在上面提到的选择中使用 np.where。

np.where 应该应用于两列。预期输出如下:

df_merge = pd.DataFrame({'column1': ['a', 'c', 'e'],
               'column2': ['b', 'd', 'f'],
               'column3': [0.5, 0.6, .04],
               'column4': [0.7, 0.8, 0.9],
               'Reason': ['correct, should not have', 'correct, should not have', 'correct, should not have'],
               })

非常感谢任何帮助/指导/替代方案。

【问题讨论】:

    标签: python-3.x pandas numpy


    【解决方案1】:

    condition 列表的第一个长度必须与 choices1 相同,因此长度为 2 的最后一个条件被注释(删除)。

    那么如果通过isin比较输出是条件(掩码),那么与列比较就没有意义了。

    最后一个问题是需要长度为 2 的列表,因此将 & 替换为 , 并删除了 choices1 列表中的括号以避免元组:

    condition = [(df_merge['column1'] == 'a') & df_merge['column2'].isin(bb),
                 (df_merge['column1'] == 'c') & df_merge['column2'].isin(dd)
    #             (df_merge['column1'] == 'e') & df_merge['column2'].isin(ff),
                 ]
    
    choices1 = [np.where(df_merge['column3'] >= 1, 'should not have','correct'),
                np.where(df_merge['column4'] >= 0.45, 'should not have','correct')]
    
    df_merge['Reason'] = np.select(condition, choices1, default='correct')
    print (df_merge)
      column1 column2  column3  column4           Reason
    0       a       b     0.50      0.7          correct
    1       c       d     0.60      0.8  should not have
    2       e       f     0.04      0.9          correct
    

    【讨论】:

    • 谢谢。但是,我需要检查两列,然后为每一行提供注释。那怎么办?
    • @Shri - 你能添加预期的输出数据帧吗?也许minimal, complete, and verifiable example 也有必要更改数据
    • 问题已更新为预期输出
    • @Shri - 你检查链接吗?因为如果所有值都相同,则不是minimal, complete, and verifiable example。也输出没有correctshould not have,类似于我的回答? (可能是其他值)
    猜你喜欢
    • 2017-04-10
    • 2018-03-09
    • 2020-07-21
    • 2020-09-29
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多