Python pandas dataframe:使用dataframe数据进行插值,而不进行更新。只需得到插值。

2024-05-13 20:17:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python pandas库相当陌生,在其他帖子中找不到我问题的答案。我有一个像这样的数据帧。日期是索引名,序列是列名。在

>>> MyDataframe
             Serie1  Serie2  Serie3  Serie4  Serie5 
2011-04-30      92      96     NaN     NaN     NaN  
2011-05-31     164     168      12     16      NaN
2011-06-30     238     242      90     20      88
2011-07-31     322     326     169     120     167

我想在这个数据帧内执行1D线性插值,但不修改数据帧,我只想得到结果。例如,我想确定Serie2在2011年6月10日的值是多少。函数DataFrame.interpolate()Series.interpolate()似乎只对用插值数据替换{}有用。在

是否有功能可以执行以下功能:

^{pr2}$

它只返回168和242之间的线性插值。在

提前感谢您的支持!在


Tags: 数据答案功能pandas序列nan帖子interpolate
1条回答
网友
1楼 · 发布于 2024-05-13 20:17:40

interpolate使用现有索引进行插值,因此您必须reindexdf,然后调用interpolate

In [48]:
df.reindex(pd.date_range(df.index[0], df.index[-1])).interpolate().loc['2011-06-10']

Out[48]:
Serie1    188.666667
Serie2    192.666667
Serie3     38.000000
Serie4     17.333333
Serie5           NaN
Name: 2011-06-10 00:00:00, dtype: float64
完成_后_ , _您_可以_选择_特定_的_日期_和_列_ :_ ^{pr2}$

这里我使用索引中的第一个和最后一个值生成一个新的datetimeindex,使用^{}。在

只在范围内的现有索引值之间进行插值会更有效。在

我们可以使用^{}找到索引值的下界:

^{3}$

相关问题 更多 >