Python Pandas - 使用多項式插值填充 NaN


若要透過多項式插值填充 NaN,請使用 Pandas 系列上的 interpolate() 方法。使用此方法,將“method”引數設定為“polynomial”。

首先,匯入必要的庫 −

import pandas as pd
import numpy as np

使用一些 NaN 值建立一個 Pandas 系列。我們已使用 numpy np.nan 設定 NaN −

d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100])

使用 interpolate() 方法的方法引數查詢多項式插值 −

d.interpolate(method='polynomial', order=2)

示例

以下為程式碼 −

import pandas as pd
import numpy as np

# pandas series
d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100])

print"Series...\n",d

# interpolate
print"\nPolynomial Interpolation...\n",d.interpolate(method='polynomial', order=2)

輸出

這將產生以下輸出 −

Series...
0   10.0
1   20.0
2    NaN
3   65.0
4   75.0
5   85.0
6    NaN
7  100.0
dtype: float64

Polynomial Interpolation...
0   10.000000
1   20.000000
2   42.854015
3   65.000000
4   75.000000
5   85.000000
6   93.532847
7  100.000000
dtype: float64

更新於: 2021 年 09 月 20 日

檢視 769 次

開啟你的 職業生涯

完成本教程並獲取認證

立即開始
廣告
© . All rights reserved.