Python Pandas -使用线性插值填充 NaN
若要使用线性插值填充 NaN,请对 Pandas 数列使用 interpolate() 方法。首先,导入必需的库 −
import pandas as pd import numpy as np
创建一个包含一些 NaN 值的 Pandas 数列。我们已使用 numpy np.nan 设置了 NaN −
d = pd.Series([10, 20, np.nan, 40, 50, np.nan, 70, np.nan, 90, 100])
查找线性插值 −
d.interpolate()
示例
以下为代码 −
import pandas as pd import numpy as np # pandas series d = pd.Series([10, 20, np.nan, 40, 50, np.nan, 70, np.nan, 90, 100]) print"Series...\n",d # interpolate print"\nLinear Interpolation...\n",d.interpolate()
输出
这将生成以下输出 −
Series... 0 10.0 1 20.0 2 NaN 3 40.0 4 50.0 5 NaN 6 70.0 7 NaN 8 90.0 9 100.0 dtype: float64 Linear Interpolation... 0 10.0 1 20.0 2 30.0 3 40.0 4 50.0 5 60.0 6 70.0 7 80.0 8 90.0 9 100.0 dtype: float64
广告