pythonpandas

Join/Merge two pandas dataframes and filling


I have two pandas dataframes both holding irregular timeseries data. I want merge/join them by time.

I also want to forward fill the other columns of frame2 for any "new" rows that were added through the joining process. How can I do this?

I have tried:

df = pd.merge(df1, df2, on="DateTime")

but this just leave a frame with matching timestamp rows.


Solution

  • Try this. The how='left' will have the merge keep all records of df1, and the fillna will populate missing values.

    df = pd.merge(df1, df2, on='DateTime', how='left').fillna(method='ffill')