我有两个数据框A,B

“ A”数据帧由1列组成(列名称:str1)

“ B”数据帧由2列组成(列名称:m2,m3)

我正在比较来自“ A”的str1与“ B”的m2

我想要的是当“ str1”与“ m2”匹配时,我希望“ str1”被“ m3”代替。
以下是我的数据,

    data frame A.str1="gmaps","facebook","gmail","linkedin"
    data frame B.m1  ="gmaps","oracle","gmail","intel"
    data frame B.m2  ="Google","xyz","Google","nvidia"


我希望输出为

    data frame A.str1="Google","facebook","Google","linkedin"


因此,只要A.str1与B.m1匹配,A.str1就会被B.m2替换
提前致谢

最佳答案

我认为您需要创建帮助器Seriesreplace或使用map,但是它会为不匹配的列创建NaN,因此有必要fillnacombine_first

df1 = pd.DataFrame({'str1': ["gmaps","facebook","gmail","linkedin"]})

df2 = pd.DataFrame({'m1': ["gmaps","oracle","gmail","intel"],
                    'm2': ["Google","xyz","Google","nvidia"]})

s = df2.set_index('m1')['m2']
print (s)
m1
gmaps     Google
oracle       xyz
gmail     Google
intel     nvidia
Name: m2, dtype: object

df1['str1'] = df1['str1'].replace(s)
#alternatives
#df1['str1'] = df1['str1'].map(s).fillna(df1['str1'])
#df1['str1'] = df1['str1'].map(s).combine_first(df1['str1'])
print (df1)

       str1
0    Google
1  facebook
2    Google
3  linkedin

关于python - 将匹配的记录替换为 Pandas 下一列中的记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49216516/

10-12 23:14