外部在Python中使用不相等的日期时间索引连接2个数据帧

时间:2018-01-24 13:25:38

标签: python pandas datetime

我有以下两个数据帧:

df1
                          Feb-14 
    date                                 
    2014-01-02 12:00:00       NaN    
    2014-01-02 16:00:00 -0.075000     
    2014-01-02 20:00:00 -0.075000     
    2014-01-03 00:00:00 -0.075000     
    2014-01-03 04:00:00 -0.069125  

df2
                         Nov-15
    date                       
    2015-07-21 08:00:00     NaN
    2015-07-21 12:00:00  -0.175
    2015-07-21 16:00:00  -0.175
    2015-07-21 20:00:00  -0.175
    2015-07-22 00:00:00  -0.175

当我使用df1 = df1.join(df2)

我明白了:

df1
                       Feb-14  Nov-15
date                                 
2014-01-02 12:00:00       NaN     NaN
2014-01-02 16:00:00 -0.075000     NaN
2014-01-02 20:00:00 -0.075000     NaN
2014-01-03 00:00:00 -0.075000     NaN
2014-01-03 04:00:00 -0.069125     NaN

我真正想要的是:

df1
                       Feb-14  Nov-15
date                                 
2014-01-02 12:00:00       NaN     NaN
2014-01-02 16:00:00 -0.075000     NaN
2014-01-02 20:00:00 -0.075000     NaN
2014-01-03 00:00:00 -0.075000     NaN
2014-01-03 04:00:00 -0.069125     NaN 
2015-07-21 08:00:00      NaN      NaN 
2015-07-21 12:00:00      NaN     -0.175
2015-07-21 16:00:00      NaN     -0.175
2015-07-21 20:00:00      NaN     -0.175
2015-07-22 00:00:00      NaN     -0.175

我怎样才能做到这一点?如果我在数据框中更改某些内容,我也无法合并?

1 个答案:

答案 0 :(得分:0)

外部联接有多种解决方案:

for (int i = 0; i < list.size() - n; i++) {
    System.out.println(ss.indexOf(list.get(i)) 
        + " thru " + ss.lastIndexOf(list.get(i + n - 1)) + " : " 
        + list.get(i));

    }