Pandas 将vlookup应用于数据帧的每个元素
我有两个数据帧,一个是源(src),另一个是目标(dest) 对于dest中的每个值,我想将其替换为src表中的一个值,该表具有与自身相同的索引和列名 e、 g.目前dest表中“2020-06-26”的AJ值为3.5。我想用src表中对应于索引“2020-06-26”和列=3.5的值替换它 我曾考虑使用applymap,但它似乎没有索引的概念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: ("'
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个问题:
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()