我必须加入2个文件基于公共列(年,月,日)。输入文件有标题,输出文件有标题,因为well.The第一个文件有关于温度的信息,如(示例):
year month day min-temp mav-temp
2017 12 13 13 25
2017 12 16 25 32
2017 12 25 34 56
第2个文件的信息沉淀如(示例)
year month day precipitation snow snow-cover
2018 7 6 0.00 0.0 0
2017 12 13 0.04 0.0 0
2017 12 16 0.4 0.04 1
我的预期输出应该是(按日期异步排序的,如果没有找到值,则为空):
year month day min-temp mav-temp precipitation snow snow-cover
2017 12 13 13 25 0.04 0.0 0
2017 12 16 25 32 0.4 0.04 1
2017 12 25 34 56
2018 7 6 0.00 0.0 0
我可以得到关于如何在Python中做到这一点的帮助吗?
发布于 2018-08-20 02:27:06
您可以尝试使用merge
、sort_values
和fillna
new_df = (df1.merge(df2, on=['year','month','day'], how='outer')
.sort_values(['year','month','day'])
.fillna(''))
>>> new_df
year month day min-temp mav-temp precipitation snow snow-cover
0 2017 12 13 13 25 0.04 0 0
1 2017 12 16 25 32 0.4 0.04 1
2 2017 12 25 34 56
3 2018 7 6 0 0 0
https://stackoverflow.com/questions/51920606
复制相似问题