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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP