给定一个考拉数据框:
df = ks.DataFrame({"high_risk": [0, 1, 0, 1, 1],
"medium_risk": [1, 0, 0, 0, 0]
})
运行 lambda 函数以根据现有列值获取新列:
df = df.assign(risk=lambda x: "High" if x.high_risk else ("Medium" if x.medium_risk else "Low"))
df
Out[72]:
high_risk medium_risk risk
0 0 1 High
4 1 0 High
1 1 0 High
2 0 0 High
3 1 0 High
预期收益:
high_risk medium_risk risk
0 0 1 Medium
4 1 0 High
1 1 0 High
2 0 0 Low
3 1 0 High
为什么这会将“高”分配给每个值。目的是对每一行进行操作,是否在比较中查看整列?