简体   繁体   English

使用 pandas - python 合并两个数据帧

[英]Merge two dataframes using pandas - python

First of all thank you for your help.首先感谢您的帮助。

I have two dataframes row indexed by date (DD-MM-YYYY HH:MM) as follows:我有两个按日期(DD-MM-YYYY HH:MM)索引的数据框行,如下所示:

DF1
        date               temp       wind 
0   31-12-2002 23:00       12.3       80   
1   01-01-2004 00:00       15.2       NAN
2   01-01-2004 01:00       18.4       NAN 
                 ........ 
DF2
        date               temp       wind 
0   31-12-2002 23:00       14.5       86   
1   01-01-2003 00:00       28.7       98
2   01-01-2003 01:00       26.7       88
                ........
n   01-01-2004 00:00       34.5       23 
m   01-01-2004 01:00       35.7       NAN 

MergedDF
        date               temp       wind 
0   31-12-2002 23:00       12.3       80
1   01-01-2003 00:00       28.7       98
2   01-01-2003 01:00       26.7       88
                ........
n   01-01-2004 00:00       15.2       23 
m   01-01-2004 01:00       18.4       NAN 

In DF1 there's one whole year (2003) missing and also some NAN values in the rest of the years.在 DF1 中缺少一整年(2003 年),并且这些年份的 rest 中还有一些 NAN 值。 Basically I want to merge both dataframes, adding the year missing and replacing NAN values if this information is in DF2.基本上我想合并两个数据框,添加缺少的年份并替换 NAN 值(如果此信息在 DF2 中)。

Someone could help me?有人可以帮助我吗? I don't know very well how to implement this on pyhton/pandas.我不太清楚如何在 pyhton/pandas 上实现这一点。

MergedDF = df1.append(df2).groupby('date', as_index=False).first()

as_index=False option of group_by is useful to keep the same table index in the aggregated output. group_byas_index=False选项有助于在聚合的 output 中保持相同的表索引。

.first() will keep the first non-null value for each date. .first()将保留每个日期的第一个非空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM