我在df
中有一个包含新IDs
的数据框dfOld
我有新旧IDs
df
ID1new ID2new
0 5 3
1 4 2
2 3 7
dfOld
IDold IDnew
0 33 0
1 78 1
2 65 2
3 12 3
4 24 4
5 89 5
6 77 6
7 16 7
8 69 8
我想向df
添加两列,其中包含IDs
和ID1new
的旧ID2new
,所以
df1
ID1new ID2new ID1old ID2old
0 5 3 89 12
1 4 2 24 65
2 3 7 12 16
我粗暴地做了这样的循环:
df1 = df
df1['ID1old']=0
df1['ID2old']=0
for i in net.index:
tmp = dfOld[dfOld.IDnew == df.ID1new[i]]
tmp0 = dfOld[dfOld.IDnew == df.ID2new[i]]
net.ID1old[i] = tmp.IDold[tmp.index[0]]
net.ID1old[i] = tmp0.IDold[tmp0.index[0]]
答案 0 :(得分:1)
您可以dict
d
或Series
s
使用map
:
d = dfOld.set_index('IDnew')['IDold'].to_dict()
print (d)
{0: 33, 1: 78, 2: 65, 3: 12, 4: 24, 5: 89, 6: 77, 7: 16, 8: 69}
#s = dfOld.set_index('IDnew')['IDold']
df['ID1old'] = df.ID1new.map(d)
df['ID2old'] = df.ID2new.map(d)
print (df)
ID1new ID2new ID1old ID2old
0 5 3 89 12
1 4 2 24 65
2 3 7 12 16