Setting DataFrame values with enlargement

前端 未结 3 1805
囚心锁ツ
囚心锁ツ 2020-12-20 16:46

I have two DataFrames (with DatetimeIndex) and want to update the first frame (the older one) with data from the second frame (the newer one).

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 17:35

    You can use the combine function.

    import pandas as pd
    
    # your data
    # ===========================================================
    df1 = pd.DataFrame(np.ones(12).reshape(4,3), columns='A B C'.split(), index=pd.date_range('2015-07-09 12:00:00', periods=4, freq='H'))
    
    df2 = pd.DataFrame(np.ones(16).reshape(4,4)*2, columns='A B C D'.split(), index=pd.date_range('2015-07-09 14:00:00', periods=4, freq='H'))
    
    # processing
    # =====================================================
    # reindex to populate NaN
    result = df2.reindex(np.union1d(df1.index, df2.index))
    
    Out[248]: 
                          A   B   C   D
    2015-07-09 12:00:00 NaN NaN NaN NaN
    2015-07-09 13:00:00 NaN NaN NaN NaN
    2015-07-09 14:00:00   2   2   2   2
    2015-07-09 15:00:00   2   2   2   2
    2015-07-09 16:00:00   2   2   2   2
    2015-07-09 17:00:00   2   2   2   2
    
    combiner = lambda x, y: np.where(x.isnull(), y, x)
    
    # use df1 to update result
    result.combine(df1, combiner)
    
    Out[249]: 
                         A  B  C   D
    2015-07-09 12:00:00  1  1  1 NaN
    2015-07-09 13:00:00  1  1  1 NaN
    2015-07-09 14:00:00  2  2  2   2
    2015-07-09 15:00:00  2  2  2   2
    2015-07-09 16:00:00  2  2  2   2
    2015-07-09 17:00:00  2  2  2   2
    
    # maybe fillna(method='ffill') if you like
    

提交回复
热议问题