我有一个关于2009年至2019年地区层面家庭数量的数据集。数据集非常完整,但缺少一些数据。 例如,我有两个区域,IE01和IE04:
n2hn_df.loc['IE01']
Out[2]:
2009 455300.0
2010 460600.0
2011 465500.0
2012 NaN
2013 NaN
2014 NaN
2015 NaN
2016 NaN
2017 NaN
2018 NaN
2019 NaN
Name: IE01, dtype: float64
n2hn_df.loc['IE04']
Out[3]:
2009 NaN
2010 NaN
2011 NaN
2012 320700.0
2013 315300.0
2014 310500.0
2015 307500.0
2016 315400.0
2017 323300.0
2018 329300.0
2019 339700.0
Name: IE04, dtype: float64
我想用一个线性外推来完成数据集(因为家庭数量在过去几年中不会发生剧烈变化)。我知道插值很容易,但有点像
n2hn_df.interpolate(method='linear',axis=1,limit_direction='both',inplace=True)
仅使用两个方向上最接近的值填充数据集。我还没有找到一种简单的方法来推断数据帧中的数据,所以我想征求您的意见,看看最好的方法是什么。如果你能提供帮助,我将不胜感激。提前谢谢
编辑: 我想从中推断数据帧的一个例子是:
我不久前也做过类似的事情。它不是超级漂亮,但也许你可以用它。作为示例,我使用以下DataFrame(第二个示例的修改版本):
year
是index
1。步骤是填充
NaN
的结尾部分:结果:
作为
increment
,我使用了现有diffs
的mean
。如果要使用最后一个diff
,请将其替换为:2。填充
NaN
的起始部分的步骤大致相同,只是将value
列反转,并在末尾重新反转:结果:
重要提示:该方法假设指数中没有缺口(缺失年份)
正如我所说,不是很漂亮,但对我来说很有效
(注:仅澄清上述“类似”的用法:这确实是线性外推。)
编辑
示例帧(屏幕截图中帧的前3行):
外推:
结果:
相关问题 更多 >
编程相关推荐