Python 3.x numpytypeerror:ufunc';按位u和';输入类型不支持,
我有以下数据框和列表值Python 3.x numpytypeerror:ufunc';按位u和';输入类型不支持,,python-3.x,pandas,numpy,Python 3.x,Pandas,Numpy,我有以下数据框和列表值 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] })
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 to代替IF函数:
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'],
})
非常感谢任何帮助/指导/备选方案。条件列表的第一个长度必须与选项1相同,因此最后一个条件对长度2进行了注释(删除) 然后,如果compare by
isin
输出是条件(mask),则compare with列没有意义
最后一个问题是长度为2的需要列表,因此将&
替换为,
,并删除了选项1
列表中避免元组的偏旁:
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-你能添加预期的输出数据帧吗?可能还需要使用预期更新问题的更改数据output@Shri-你检查链接了吗?因为如果所有的值都相同,那么它就不同了。另外,输出的是否
正确
或不应该有
,类似于我的回答?(可能是另一种价值观)