接上一篇文章,如何在合并相同列的csv文件的情况下,保留未匹配到的数据行?
import pandas as pd import os df01 = pd.read_csv("C:\\Users\\hanhan\\PycharmProjects\\pythonProject\\01.csv",encoding = 'gb2312',dtype='str') df02 = pd.read_csv("C:\\Users\\hanhan\\PycharmProjects\\pythonProject\\02.csv",encoding = 'gb2312',dtype='str') df_pri = pd.merge(df01,df02,on='date',how='outer') df_pri.to_csv('0102.csv',header=True,index=False)
例如:
表1:
date | a | b |
2021-11-09 | 1 | 4 |
2021-11-10 | 2 | 5 |
2021-11-11 | 3 | 6 |
表2:
date | a |
2021-11-09 | 1 |
2021-11-10 | 2 |
2021-11-11 | 3 |
2021-11-12 | 7 |
运行程序得到的结果:
date | a | b |
2021-11-09 | 1 | 4 |
2021-11-10 | 2 | 5 |
2021-11-11 | 3 | 6 |
2021-11-12 | 7 | NaN |