加入/合并两个pandas数据帧并填充

azu*_*ric 3 python pandas

我有两个pandas数据帧都持有不规则的时间序列数据.

我想按时间合并/加入两个帧.

我还想向前填充frame2的其他列,以获取通过连接过程添加的任何"新"行.我怎样才能做到这一点?

我试过了:

df = pd.merge(df1, df2, on="DateTime")
Run Code Online (Sandbox Code Playgroud)

但这只留下一个匹配时间戳行的帧.

我会很感激任何想法!

chr*_*isb 8

试试这个.在how='left'将合并保持DF1的所有记录,并且fillna将填充缺失值.

df = pd.merge(df1, df2, on='DateTime', how='left').fillna(method='ffill')
Run Code Online (Sandbox Code Playgroud)