A B C D
0 foo one 0 0
1 bar one 1 2
2 foo two 2 4
3 pak three 3 6
4 foo two 4 8
5 bar two 5 10
6 foo one 6 12
7 dop three 7 14
8 foo one 0 0
9 bar one 1 2
10 pak two 2 4
11 bar three 3 6
我需要将C和D列的权重相乘,但是A列的权重不同。对于foo是2,对于bar是1,5,对于pak是3,4,对于dop是2,3。实际上,这些条件是15,数据非常庞大,所以我不能用于循环。
答案 0 :(得分:0)
使用Series.map
:
conditions = {
'foo': 2,
'bar': 1.5,
'pak': 3.4,
'dop': 2.3
}
weights = df['A'].map(conditions)
df['result'] = df['C'] * df['D'] * weights