我有两个要合并的Excel文件:
file1 file2
a b a b
1 AA aa 1 AA aaaa
2 BB bb 2 BB bbbb
3 CC cc 3 CC cccc
4 DD dd
5 EE ee
具有:
all_data_st = pd.merge( file1, file2, on="Unique")
我能够合并并接收:
file3
a b c
1 AA aa aaaa
2 BB bb bbbb
3 CC cc cccc
但是,我想收到:
file4
a b c
1 AA aa aaaa
2 BB bb bbbb
3 CC cc cccc
4 DD dd
5 EE ee
请告知公式。谢谢
答案 0 :(得分:1)
将您的数据框视为
file1 = pd.DataFrame({"a": ["AA", "BB", "CC", "DD", "EE"], "b": ["aa", "bb", "cc", "dd", "ee"]})
file2 = pd.DataFrame({"a": ["AA", "BB", "CC"], "b": ["aaaa", "bbbb", "cccc"]})
使用:
all_data_st = (
pd.merge(file1, file2, on="a", how="left")
.fillna('').rename(columns={"b_x": "b", "b_y": "c"}))
print(all_data_st)
结果:
a b c
0 AA aa aaaa
1 BB bb bbbb
2 CC cc cccc
3 DD dd
4 EE ee