Pandas:如何合并两个数据框?

数据挖掘 熊猫
2021-09-28 00:06:26

我找到了(如何在 Python Pandas 中合并两个数据框?),但没有得到预期的结果。

我有这两个 CSV 文件:

# f1.csv
num   ano
76971  1975
76969  1975
76968  1975
76966  1975
76964  1975
76963  1975
76960  1975

# f2.csv
num   ano   dou  url
76971  1975 p1   http://exemplo.com/page1
76968  1975 p2   http://exemplo.com/page10
76966  1975 p2   http://exemplo.com/page100

如何合并这些以获得下面给出的结果?

# Expected result
num   ano   dou  url
76971  1975 p1   http://exemplo.com/page1
76969  1975
76968  1975 p2   http://exemplo.com/page10
76966  1975 p2   http://exemplo.com/page100
76964  1975
76963  1975
76960  1975
1个回答

f1.merge(f2, left_on='num', right_on='num', how='outer')

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html