以下两个数据框:
df_t1 = pd.DataFrame({'Date' : [pd.Timestamp(2017, 1, 1), pd.Timestamp(2017, 1, 2), pd.Timestamp(2017, 1, 3)], 'A': [1, 2, 3]})
df_t1 = df_t1.set_index('Date')
df_t2 = pd.DataFrame({'Date' : [pd.Timestamp(2016, 12, 31), pd.Timestamp(2017, 1, 1), pd.Timestamp(2017, 1, 2), pd.Timestamp(2017, 1, 3), pd.Timestamp(2017, 1, 4)], 'C': [7, 8, 9, 10, 11]})
df_t2 = df_t2.set_index('Date')
可以像这样合并
df_merge = df_t1.merge(df_t2, how='outer', left_on='Date', right_on='Date')
或类似
df_merge2 = df_t2.merge(df_t1, how='outer', left_on='Date', right_on='Date')
我希望两个变体都返回df_merge2,如下所示,为什么不是这样?
C A
Date
2016-12-31 7 NaN
2017-01-01 8 1.0
2017-01-02 9 2.0
2017-01-03 10 3.0
2017-01-04 11 NaN