其中值是0,我正在对它进行除法,这似乎抛出错误。如何避免这个错误
public-read
新列仅填充“异常”,如何纠正以上代码
预期产量
wt pred wt remarks
0 14 Anomaly
0 20 Anomaly
25 30 Anomaly
22 21 Anomaly
21 102 Anomaly
def valuation_formula(x,y):
if float(abs(x-y)/y*100) > 25.0:
return "Anomaly"
else :
return "Pass"
try:
df_Wt['Weight_Remarks'] = df_Wt.apply(lambda row:
valuation_formula(row['Predicted Weight'], row['Weight']), axis=1)
except ZeroDivisionError:
df_Wt['Weight_Remarks'] = "Anomaly"
答案 0 :(得分:2)
df['remarks'] = np.where(((abs(df['pred wt']-df['wt']))/df['wt']).gt(0.25), "Weight Anomaly", 'Pass')
答案 1 :(得分:0)
使用update()
:
numpy.where
输出:
import numpy as np
df['new_remarks'] = np.where(df['wt'].ne(0), df['pred wt']/df['wt'], 'Anomaly')
print(df)
答案 2 :(得分:0)
尝试此代码
df['remarks']= np.where(df.wt.div(df.pred,fill_value=1).eq(0),'Anamoly',np.where(((abs(df['pred']-df['wt']))/df['wt']).lt(0.25), "Weight Anomaly", 'Pass'))
我认为您输入的输出与功能不匹配。至少一个值应为“ Weight Anamoly”。调整lt(0.25)
以获取所需的结果。 lt代表“小于”,您可以将其更改为“ gt”(大于)以满足您的需求