我有3个dfs:
DF1:
Zip County Zipcount Date
111 A 3 1/1/17
111 A 4 1/2/17
112 B 5 1/1/17
113 B 3 1/2/17
DF2:
County Countycount Date
A 5 1/1/17
A 6 1/2/17
B 1 1/1/17
C 4 1/1/17
df3(参考表):
Zip County
111 A
112 B
113 B
115 C
目标:
Zip County Zipcount Countycount Date
111 A 3 5 1/1/17
111 A 4 6 1/2/17
112 B 5 1 1/1/17
113 B 3 NA 1/2/17
115 C NA 4 1/1/17
我尝试按日期和县合并df1和df2,但我无法通过引用来填写缺少的日期。 感谢您的帮助!
答案 0 :(得分:1)
merge
+ fillna
g=df1.merge(df2,how='outer')
g['Zip']=g.set_index('County').Zip.fillna(df3.set_index('County').Zip.to_dict()).values
g
Out[1014]:
Zip County Zipcount Date Countycount
0 111.0 A 3.0 1/1/17 5.0
1 111.0 A 4.0 1/2/17 6.0
2 112.0 B 5.0 1/1/17 1.0
3 113.0 B 3.0 1/2/17 NaN
4 115.0 C NaN 1/1/17 4.0