用相应的值替换特定值 [英] Replace value in a specific with corresponding value

查看:96
本文介绍了用相应的值替换特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为REF的数据框,其结构如下:

I have a dataframe called REF with the following structure:

old_id  new_id
     3       6
     4       7
     5       8

我想替换所有可以找到的值,使其等于另一个数据帧NEW中的任何old_id值,即:

I want to replace all the values that can be found equal to any of the old_id values in another dataframe NEW that is:

old_id  column_1  column_2
     3         a         e
     4         b         f
     9         c         g
     9         d         h

因此,新的输出数据集NEW将为:

Therefore the new output dataset NEW will be:

old_id  column_1  column_2
     6         a         e
     7         b         f
     9         c         g
     9         d         h

推荐答案

使用 map :

s = df1.set_index('old_id')['new_id']

df2['old_id'] = df2['old_id'].map(s).fillna(df2['old_id'])

或者 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆