Pandas 将vlookup应用于数据帧的每个元素

Pandas 将vlookup应用于数据帧的每个元素,pandas,numpy,dataframe,apply,Pandas,Numpy,Dataframe,Apply,我有两个数据帧,一个是源(src),另一个是目标(dest) 对于dest中的每个值,我想将其替换为src表中的一个值,该表具有与自身相同的索引和列名 e、 g.目前dest表中“2020-06-26”的AJ值为3.5。我想用src表中对应于索引“2020-06-26”和列=3.5的值替换它 我曾考虑使用applymap,但它似乎没有索引的概念 dest.applymap(lambda x: src.loc[x.index][x]).tail() AttributeError: ("'

我有两个数据帧,一个是源(src),另一个是目标(dest)

对于dest中的每个值,我想将其替换为src表中的一个值,该表具有与自身相同的索引和列名

e、 g.目前dest表中“2020-06-26”的AJ值为3.5。我想用src表中对应于索引“2020-06-26”和列=3.5的值替换它

我曾考虑使用applymap,但它似乎没有索引的概念

dest.applymap(lambda x: src.loc[x.index][x]).tail()
AttributeError: ("'numpy.float64' object has no attribute 'index'", u'occurred at index AJ')
然后我尝试使用apply,它的工作原理如下:

dest1 = dest.replace(0,np.nan).fillna(1)  # 0 and nan are not in src.columns
df= dest1.apply(lambda x: [src[col].loc[row] for row, col in zip(x.index,x)], axis=0).tail()
关于这一点的2个问题:

  • 有没有更好的解决方案,而不是在apply中进行列表理解
  • 有没有更好的方法来处理dest中不在src.columns(如0和nan)中的值,以便在这种情况下输出为nan
  • 请不要上载图像:请不要上载图像:
    dest1 = dest.replace(0,np.nan).fillna(1)  # 0 and nan are not in src.columns
    df= dest1.apply(lambda x: [src[col].loc[row] for row, col in zip(x.index,x)], axis=0).tail()