1 回答
TA贡献2021条经验 获得超8个赞
最佳做法是对性能进行矢量化计算。因此我用过np.where
反转你的逻辑,当百分比小于等于零且状态不是Erisa时默认为零
如果百分比 >零则标准计算
留下一个案例,它是Erisa并且百分比 <零
为了更好地衡量,我添加了一个列检查以更好地测试逻辑是否按定义工作
r = 10
df = pd.DataFrame({"ERISA Percentage":[random.randint(-25,100) for i in range(r)],
"Net Amount (Local)":[random.randint(50,125) for i in range(r)],
"ERISA Status":[["Erisa","a","b"][random.randint(0,2)] for i in range(r)]
})
df = df.assign(**{"Erisa (Local)":lambda dfa:
np.where((dfa["ERISA Percentage"]<=0) & (dfa["ERISA Status"]!="Erisa"), 0,
np.where(dfa["ERISA Percentage"]>0,
dfa["ERISA Percentage"]/100*dfa["Net Amount (Local)"],
dfa["Net Amount (Local)"])),
"check":lambda dfa: dfa["Erisa (Local)"]==dfa["Net Amount (Local)"]
})
print(df.to_string(index=False))
输出
ERISA Percentage Net Amount (Local) ERISA Status Erisa (Local) check
79 56 Erisa 44.24 False
68 124 b 84.32 False
33 124 Erisa 40.92 False
-21 100 b 0.00 False
42 124 b 52.08 False
-5 76 b 0.00 False
38 95 Erisa 36.10 False
20 109 b 21.80 False
55 63 a 34.65 False
-13 106 Erisa 106.00 True
添加回答
举报