pandas.merge: match the nearest time stamp >= the series of timestamps

后端 未结 4 1090
再見小時候
再見小時候 2020-11-29 03:16

I have two dataframes, both of which contain an irregularly spaced, millisecond resolution timestamp column. My goal here is to match up the rows so that for each matched ro

4条回答
  •  醉话见心
    2020-11-29 03:40

    I used a different way than HYRY:

    1. do a regular merge with outer join (how='outer');
    2. sort it by date;
    3. use fillna(method='pad') to take fill just the columns you need and 'pad' if you would like to take the previous filled row;
    4. drop all the rows you don't need from the outer join.

    All this can be written in few lines:

    df=pd.merge(df0, df1, on='Date', how='outer')   
    df=df.sort(['Date'], ascending=[1])
    headertofill=list(df1.columns.values)
    df[headertofill]=df[headertofill].fillna(method='pad')
    df=df[pd.isnull(df[var_from_df0_only])==False] 
    

提交回复
热议问题