首页 > 解决方案 > 如何在 Python 中进行插值?

问题描述

我想对我的数据帧 df2 中的 Value_B 列进行线性插值。如何以简单的方式使用 python 做到这一点?

df2 = pd.DataFrame(np.array([[1, 2, 10], [2, 5, ''], [3, 8, 30], [4, 2, ''], [5, 5, 50], [6, 8, '']]), columns=['Angle', 'Value_A', 'Value_B'])

df2

Value_B 的结果应该是 10, '20', 30, '40', 50, '60'。

标签: pythonlinear-interpolation

解决方案


熊猫 interpolate()功能

df2.interpolate(method ='linear', limit_direction ='forward') 

您甚至可以向后插值并设置限制

df.interpolate(method ='linear', limit_direction ='backward', limit = 1)

推荐阅读