如何在Python中插值?

如何在Python中插值?,python,linear-interpolation,Python,Linear Interpolation,我想对数据帧df2中的列值_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 值B的结果应为10,'20',30,'40',50,'60。 df2.插值(方法='线性',极限\方向='前进')

我想对数据帧df2中的列值_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

值B的结果应为10,'20',30,'40',50,'60。

df2.插值(方法='线性',极限\方向='前进')
您甚至可以向后插值并设置限制

df.插值(方法='线性',限制\方向='向后',限制=1)

我意识到,在插值之前,我们需要先将df2的列设置为数值。 然后我们可以按照@leopardxpresponse进行操作

for col in df2.columns:
    df2[col] = pd.to_numeric(df2[col], errors='coerce')
df = df2.interpolate(method ='linear', limit_direction ='forward')

熊猫有一个
interpolate()
函数
df2['Value_B']=df2['Value_B'].apply(pd.to_numeric).replace('',np.nan, regex=True).interpolate() #empty cells need to be Nan