【问题标题】:Pandas data replace issue [duplicate]熊猫数据替换问题[重复]
【发布时间】:2018-07-27 03:57:21
【问题描述】:
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

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用pd.Series.map

    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
    

    【讨论】:

      猜你喜欢
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多