简体   繁体   中英

python pandas concatening 2 timeseries dataframes

I have the 2 following dataframes:
df1:

               datetime   actual    forecast    previous
0   2015-01-09 13:30:00     -0.2         0.2         0.2
1   2015-02-06 13:30:00      0.5         0.3        -0.2
2   2015-03-06 13:30:00      0.1         0.2         0.5
3   2015-04-03 12:30:00      0.3         0.2         0.1
4   2015-05-08 12:30:00      0.1         0.2         0.2

and df2:

            datetime    Actual  Surv(M) Prior
0   2015-01-09 08:30    -0.2%   0.2%    0.4%
1   2015-02-06 08:30    0.5%    0.3%    -0.2%
2   2015-03-06 08:30    0.1%    0.2%    0.5%
3   2015-04-03 08:30    0.3%    0.2%    0.1%
4   2015-05-08 08:30    0.1%    0.2%    0.3%

I am trying to concatenate them so that they appear in a dataframe ordered based on their timestamp.

To do so i am doing:

df1.set_index('datetime', drop=False, inplace=True)
df2.set_index('datetime', drop=False, inplace=True)
pd.concat([df1, df2], axis=1)

But this is the result:

                                 datetime   actual  forecast    previous     datetime   Actual  Surv(M)  Prior
datetime                                
2015-01-09 08:30:00                   NaT      NaN       NaN         NaN          NaN   NaN         NaN    NaN
2015-01-09 13:30:00   2015-01-09 13:30:00     -0.2       0.2         0.2          NaN   NaN         NaN    NaN
2015-02-06 08:30:00                   NaT      NaN       NaN         NaN          NaN   NaN         NaN    NaN
2015-02-06 13:30:00   2015-02-06 13:30:00      0.5       0.3        -0.2          NaN   NaN         NaN    NaN
2015-03-06 08:30:00                   NaT      NaN       NaN         NaN          NaN   NaN         NaN    NaN
2015-03-06 13:30:00   2015-03-06 13:30:00      0.1       0.2         0.5          NaN   NaN         NaN    NaN
2015-04-03 08:30:00                   NaT      NaN       NaN         NaN          NaN   NaN         NaN    NaN
2015-04-03 12:30:00   2015-04-03 12:30:00      0.3       0.2         0.1          NaN   NaN         NaN    NaN
2015-05-08 08:30:00                   NaT      NaN       NaN         NaN          NaN   NaN         NaN    NaN
2015-05-08 12:30:00   2015-05-08 12:30:00      0.1       0.2         0.2          NaN   NaN         NaN    NaN

The data coming from df2 is missing. It seems that the rows for it are correctly allocated judging by the rythm in the data coming from df1, but it does not appear.

Any suggestion to have df2 data beeing there correctly?

Edit: the desired result:

                                     datetime   actual  forecast    previous     datetime   Actual  Surv(M)  Prior
    datetime                                
2015-01-09 08:30:00                   NaT      NaN       NaN         NaN              2015-01-09 08:30    -0.2%   0.2%    0.4%
    2015-01-09 13:30:00   2015-01-09 13:30:00     -0.2       0.2         0.2          NaN   NaN         NaN    NaN
2015-02-06 08:30:00                   NaT      NaN       NaN         NaN          2015-02-06 08:30    0.5%    0.3%    -0.2%
    2015-02-06 13:30:00   2015-02-06 13:30:00      0.5       0.3        -0.2          NaN   NaN         NaN    NaN
2015-03-06 08:30:00                   NaT      NaN       NaN         NaN          2015-03-06 08:30    0.1%    0.2%    0.5%
    2015-03-06 13:30:00   2015-03-06 13:30:00      0.1       0.2         0.5          NaN   NaN         NaN    NaN
2015-04-03 08:30:00                   NaT      NaN       NaN         NaN          2015-04-03 08:30    0.3%    0.2%    0.1%
    2015-04-03 12:30:00   2015-04-03 12:30:00      0.3       0.2         0.1          NaN   NaN         NaN    NaN
2015-05-08 08:30:00                   NaT      NaN       NaN         NaN          2015-05-08 08:30    0.1%    0.2%    0.3%
    2015-05-08 12:30:00   2015-05-08 12:30:00      0.1       0.2         0.2          NaN   NaN         NaN    NaN

Its really quite difficult to format that table here... but basically here above would be the desired output.

df1 and df2 have different column names, so the concat function do not give directly the result you want. Instead, you can change df2 columns and then concat the dataframes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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