combining three different timestamp dataframes using duration match

后端 未结 1 785
盖世英雄少女心
盖世英雄少女心 2021-01-24 10:53

I have three data frames with different dataframes and frequencies. I want to combine them into one dataframe.

First dataframe collects sunlight from sun as given below

相关标签:
1条回答
  • 2021-01-24 11:42

    Nice question I am using reindex with nearest as method 1

    df1['row']=df1.index
    s1=df1.reindex(df2.index,method='nearest')
    s2=df1.reindex(df3.index,method='nearest')
    s1=s1.join(df2).set_index('row')
    s2=s2.join(df3).set_index('row')
    
    pd.concat([s1,s2.reindex(s1.index,method='nearest')],1)
    Out[67]: 
                         light_data    A  light_data    B
    row                                                  
    2019-05-01 06:54:00          10  100          40  400
    2019-05-01 06:59:00          50  200          60  500
    2019-05-01 07:04:00          80  300          90  600
    

    Or at the last line using merge_asof

    pd.merge_asof(s1,s2,left_index=True,right_index=True,direction='nearest')
    Out[81]: 
                         light_data_x    A  light_data_y    B
    row                                                      
    2019-05-01 06:54:00            10  100            40  400
    2019-05-01 06:59:00            50  200            40  400
    2019-05-01 07:04:00            80  300            90  600
    

    Make it extendable

    df1['row']=df1.index
    
    l=[]
    for i,x in enumerate([df2,df3]):
        s1=df1.reindex(x.index,method='nearest')
        if i==0:
            l.append(s1.join(x).set_index('row').add_suffix(x.columns[0].str[-1]))
        else :
            l.append(s1.join(x).set_index('row').reindex(l[0].index,method='nearest').add_suffix(x.columns[0].str[-1]))
    pd.concat(l,1)
    
    0 讨论(0)
提交回复
热议问题