python新手,似乎无法找到我正在寻找的确切答案 我相信有一种更简便的方法来填写此信息
我有df1和df2
df1: FirstName LastName PhNo uniqueid
df2: uniqueid PhNo
我想根据匹配的uniqueid == uniqueid用df2中的匹配值填充df1 ['PhNo']中缺少的值
我使用的代码如下
dff = pd.merge(df1,df2,on = 'uniqueid', how = 'Left')
dff['PhNo'] = 0
dff['PhNo'][df1['PhNo_x'] >= 1] = df1['PhNo_x']
df1['PhNo'][df2['PhNo_y'] >= 1] = df1['PhNo_y']
这似乎可以完成工作,但似乎不是一种有效的方法。我正在寻找比合并更少的行和更好的技术
df1
FirstName LastName PhNo uniqueid
Sam R 123x 1
John S 345x 2
Paul K np.Nan 3
Laney P no.NaN 4
df2
uniqueid PhNo
1 213x
3 675x
4 987x
所需输出:df1
FirstName LastName PhNo uniqueid
Sam R 123x 1
John S 345x 2
Paul K **675x** 3
Laney P **987x** 4
答案 0 :(得分:4)
我相信您需要Series.map
和Series.fillna
:
df1 = pd.DataFrame({
'FirstName':list('abcdef'),
'LastName':list('aaabbb'),
'PhNo':[7,np.nan,9,4,np.nan,np.nan],
'uniqueid':[5,3,6,9,2,4],
})
print (df1)
FirstName LastName PhNo uniqueid
0 a a 7.0 5
1 b a NaN 3
2 c a 9.0 6
3 d b 4.0 9
4 e b NaN 2
5 f b NaN 4
df2 = pd.DataFrame({
'PhNo':[10,90,30,20],
'uniqueid':[3,6,9,4],
})
print (df2)
PhNo uniqueid
0 10 3
1 90 6
2 30 9
3 20 4
s = df2.set_index('uniqueid')['PhNo']
df1['PhNo'] = df1['PhNo'].fillna(df1['uniqueid'].map(s))
print (df1)
FirstName LastName PhNo uniqueid
0 a a 7.0 5
1 b a 10.0 3
2 c a 9.0 6
3 d b 4.0 9
4 e b NaN 2
5 f b 20.0 4