合并/连接/联接具有重复列但具有不同Datetime索引的两个DataFrame的最佳方法?

时间:2019-03-27 15:54:23

标签: python pandas

我有两个数据框,其中一个包含过去的数据。另一个带有预测。我想合并它们,以便没有重复的列。

我的代码如下:

Past = 
                      X RealData
2019-03-27 12:30:00 8.295   True
2019-03-27 13:00:00 7.707   True
2019-03-27 13:30:00 7.518   True
2019-03-27 14:00:00 7.518   True
2019-03-27 14:30:00 7.518   True
2019-03-27 15:00:00 7.455   True
2019-03-27 15:30:00 7.518   True
2019-03-27 16:00:00 20.244  True
2019-03-27 16:30:00 20.895  True
2019-03-27 17:00:00 21.630  True
2019-03-27 17:30:00 24.360  True
2019-03-27 18:00:00 24.591  True
2019-03-27 18:30:00 26.460  True
2019-03-27 19:00:00 14.280  True
2019-03-27 19:30:00 12.180  True
2019-03-27 20:00:00 11.550  True
2019-03-27 20:30:00 9.051   True
2019-03-27 21:00:00 8.673   True
2019-03-27 21:30:00 7.791   True

Future = 
                        X           RealData
2019-03-27 22:30:00 8.450913    False
2019-03-27 23:00:00 8.494944    False
2019-03-27 23:30:00 9.058649    False
2019-03-28 00:00:00 22.055525   False
2019-03-28 00:30:00 23.344284   False
2019-03-28 01:00:00 24.793011   False
2019-03-28 01:30:00 26.203117   False
2019-03-28 02:00:00 27.897289   False
2019-03-28 02:30:00 14.187933   False
2019-03-28 03:00:00 14.110393   False

此刻,我正在尝试:

past_future = pd.concat([Future, Past], axis=1, sort=True)

我正在得到这个:

                  X RealData    X   RealData
2019-03-27 12:30:00 8.295   True    NaN NaN
2019-03-27 13:00:00 7.707   True    NaN NaN
2019-03-27 13:30:00 7.518   True    NaN NaN
2019-03-27 14:00:00 7.518   True    NaN NaN
2019-03-27 14:30:00 7.518   True    NaN NaN
2019-03-27 15:00:00 7.455   True    NaN NaN
2019-03-27 15:30:00 7.518   True    NaN NaN
2019-03-27 16:00:00 20.244  True    NaN NaN
2019-03-27 16:30:00 20.895  True    NaN NaN
2019-03-27 17:00:00 21.630  True    NaN NaN
2019-03-27 17:30:00 24.360  True    NaN NaN
2019-03-27 18:00:00 24.591  True    NaN NaN
2019-03-27 18:30:00 26.460  True    NaN NaN
2019-03-27 19:00:00 14.280  True    NaN NaN
2019-03-27 19:30:00 12.180  True    NaN NaN
2019-03-27 20:00:00 11.550  True    NaN NaN
2019-03-27 20:30:00 9.051   True    NaN NaN
2019-03-27 21:00:00 8.673   True    NaN NaN
2019-03-27 21:30:00 7.791   True    NaN NaN
2019-03-27 22:30:00 NaN NaN 8.450913    False
2019-03-27 23:00:00 NaN NaN 8.494944    False
2019-03-27 23:30:00 NaN NaN 9.058649    False
2019-03-28 00:00:00 NaN NaN 22.055525   False
2019-03-28 00:30:00 NaN NaN 23.344284   False
2019-03-28 01:00:00 NaN NaN 24.793011   False
2019-03-28 01:30:00 NaN NaN 26.203117   False
2019-03-28 02:00:00 NaN NaN 27.897289   False
2019-03-28 02:30:00 NaN NaN 14.187933   False
2019-03-28 03:00:00 NaN NaN 14.110393   False

我的预期输出只有两列:

                      X         RealData
2019-03-27 12:30:00 8.295   True
2019-03-27 13:00:00 7.707   True
2019-03-27 13:30:00 7.518   True
2019-03-27 14:00:00 7.518   True
...                 ...         ...
2019-03-27 22:30:00 8.450913    False
2019-03-27 23:00:00 8.494944    False
2019-03-27 23:30:00 9.058649    False

有人知道如何处理吗?

2 个答案:

答案 0 :(得分:2)

我的简单建议-保持一切井井有条。 然后一切都变得容易。

For Each f As Form In My.Application.OpenForms
    If TypeOf f Is frmCustomerPurchaseOrders Then
        ' ... do something in here with "f" ...
    End If
Next

enter image description here

import pandas as pd

df1 = pd.read_csv('c:/4/a1.csv')
df2 = pd.read_csv('c:/4/a2.csv')
df2.dtypes

enter image description here

答案 1 :(得分:0)

只是为了规范ags29在这里写的内容Best way to merge/concatenate/join two DataFrames with duplicate columns, but the different Datetime indices?

output = pd.concat([Future.reset_index(), Past.reset_index()], axis=0)
output.set_index('index', inplace=True)

尽管WojciechMoszczyński的答案要彻底得多,但这似乎做得很好。