python - 熊猫数据替换问题
问题描述
Table A :
y type
1 a
1 a
0 b
1 c
0 b
1 a
------------
Table B
type x
a 0.1
b 0.2
c 0.3
------------
我想用 B 中相应的 x 替换 A.type。因此我想要的结果是:
y type
1 0.1
1 0.1
0 0.2
1 0.3
解决方案
A['type'] = A.type.map(B.set_index('type').x)
y type
0 1 0.1
1 1 0.1
2 0 0.2
3 1 0.3
4 0 0.2
5 1 0.1