在 Python 中合并两个数据帧,将日期从一个数据帧线性插值到另一个数据帧

我有 2 个不同的数据框。一个是具有日期和值的“值”列表。第二个数据框是日期数据框,位于数据框 1 的日期之间。我想合并 2 个数据框并将数据框 1 中的值插入数据框 2 中的值。


我尝试了以下方法:


sample_final= sample_set_1.merge(sample_set_2, how='right').set_index('Maturity').interpolate()


但它不起作用!我在一个循环中转来转去!


任何帮助表示赞赏!


谢谢


成熟度

2019-04-11 1.0

2019-04-12 0.999933

2019-04-15 0.999732

2019-04-16 0.9996649999999999

2019-04-23 0.9991969999999999

2019-04-29 0.9987959999999999

2019-05-06 0.9983290000000001

2019-05-15 0.9977290000000001

2019-06-17 0.995532

2019-07-15 0.99369

2019-08-15 0.991663

2019-09-16 0.989582

2019-10-15 0.987725

2019-11-15 0.985764

2019-12-16 0.98383

2020-01-15 0.9819899999999999

2020-02-18 0.979935

2020-03-16 0.9783209999999999

2020-04-15 0.97655

2020-07-15 0.971353

2020-10-15 0.96631

2021-04-15 0.9568610000000001

2022-04-19 0.938263

2023-04-17 0.9198

2024-04-15 0.9006190000000001


慕村225694
浏览 141回答 1
1回答

慕尼黑的夜晚无繁华

假设您想在当天线性插值:df.Maturity = pd.to_datetime(df.Maturity)df2.Maturity = pd.to_datetime(df2.Maturity)# get the common date in the two data framescommon = df.merge(df2.drop_duplicates(), how='outer').set_index('Maturity')# interpolate the value:common = common.resample('d').interpolate().reset_index()# mergecommon.merge(df2, how='right')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python