Combine two Pandas dataframes, resample on one time column, interpolate

后端 未结 2 624
情深已故
情深已故 2021-01-01 15:33

This is my first question on stackoverflow. Go easy on me!

I have two data sets acquired simultaneously by different acquisition systems with different sampling rate

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 15:36

    If you construct a single DataFrame from Series, using time values as index, like this:

    >>> t1 = np.array([0, 0.5, 1.0, 1.5, 2.0])
    >>> y1 = pd.Series(t1, index=t1)
    
    >>> t2 = np.array([0, 0.34, 1.01, 1.4, 1.6, 1.7, 2.01])
    >>> y2 = pd.Series(3*t2, index=t2)
    
    >>> df = pd.DataFrame({'y1': y1, 'y2': y2})
    >>> df
           y1    y2
    0.00  0.0  0.00
    0.34  NaN  1.02
    0.50  0.5   NaN
    1.00  1.0   NaN
    1.01  NaN  3.03
    1.40  NaN  4.20
    1.50  1.5   NaN
    1.60  NaN  4.80
    1.70  NaN  5.10
    2.00  2.0   NaN
    2.01  NaN  6.03
    

    You can simply interpolate it, and select only the part where y1 is defined:

    >>> df.interpolate('index').reindex(y1)
          y1   y2
    0.0  0.0  0.0
    0.5  0.5  1.5
    1.0  1.0  3.0
    1.5  1.5  4.5
    2.0  2.0  6.0
    

提交回复
热议问题