Python Pandas - 插值處理缺失值



插值是 Pandas 中一種強大的技術,用於處理資料集中的缺失值。此技術根據資料集的其他資料點估算缺失值。Pandas 為 DataFrame 和 Series 物件都提供了 **interpolate()** 方法,可以使用各種插值方法填充缺失值。

在本教程中,我們將學習 Pandas 中的 **interpolate()** 方法,使用不同的插值方法填充時間序列資料、數值資料等中的缺失值。

基本插值

DataFrame 和 Series 物件的 Pandas **interpolate()** 方法用於使用不同的插值策略填充缺失值。預設情況下,Pandas 自動使用線性插值作為預設方法。

示例

這是一個呼叫 **interpolate()** 方法填充缺失值的簡單示例。

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Using the  interpolate() method
result = df.interpolate()
print("\nResultant DataFrame after applying the interpolation:")
print(result)

以下是上述程式碼的輸出 -

Original DataFrame:
AB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
AB
01.1000.250000
12.3001.733333
23.5003.216667
34.1754.700000
44.85010.000000
55.52514.700000
66.2001.300000
77.9009.200000

不同的插值方法

Pandas 支援多種插值方法,包括線性、多項式、pchip、akima、spline 等。這些方法為根據資料的性質填充缺失值提供了靈活性。

示例

以下示例演示了使用 **interpolate()** 方法和 **barycentric** 插值技術。

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Applying the interpolate() with Barycentric method
result = df.interpolate(method='barycentric')

print("\nResultant DataFrame after applying the interpolation:")
print(result)

以下是上述程式碼的輸出 -

Original DataFrame:
iAB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
AB
01.1000000.250000
12.59642957.242857
23.50000024.940476
34.0614294.700000
44.53142910.000000
55.16071414.700000
66.2000001.300000
77.9000009.200000

處理插值中的限制

預設情況下,Pandas 插值填充所有缺失值,但是您可以使用 **interpolate()** 方法的 **limit** 引數限制填充多少個連續的 NaN 值。

示例

以下示例演示了透過使用 **interpolate()** 方法的 **limit** 引數限制連續填充來填充 Pandas DataFrame 的缺失值。

import numpy as np
import pandas as pd

df = pd.DataFrame({"A": [1.1, np.nan, 3.5, np.nan, np.nan, np.nan, 6.2, 7.9],
"B": [0.25, np.nan, np.nan, 4.7, 10, 14.7, 1.3, 9.2],
})

print("Original DataFrame:")
print(df)

# Applying the interpolate() with limit
result = df.interpolate(method='spline', order=2, limit=1)

print("\nResultant DataFrame after applying the interpolation:")
print(result)

以下是上述程式碼的輸出 -

Original DataFrame:
iAB
01.10.25
1NaNNaN
23.5NaN
3NaN4.70
4NaN10.00
5NaN14.70
66.21.30
77.99.20
Resultant DataFrame after applying the interpolation:
iAB
01.1000000.250000
12.231383-1.202052
23.500000NaN
34.1115294.700000
4NaN10.000000
5NaN14.700000
66.2000001.300000
77.9000009.200000

時間序列資料的插值

插值也可以應用於 Pandas 時間序列資料。在填充隨時間推移缺失資料點的間隙時,這很有用。

示例

示例語句 -

import numpy as np
import pandas as pd

indx = pd.date_range("2024-01-01", periods=10, freq="D")
data = np.random.default_rng(2).integers(0, 10, 10).astype(np.float64)
s = pd.Series(data, index=indx)
s.iloc[[1, 2, 5, 6, 9]] = np.nan

print("Original Series:")
print(s)

result = s.interpolate(method="time")

print("\nResultant Time Series after applying the interpolation:")
print(result)

以下是上述程式碼的輸出 -

Original Series:
DateValue
2024-01-018.0
2024-01-02NaN
2024-01-03NaN
2024-01-042.0
2024-01-054.0
2024-01-06NaN
2024-01-07NaN
2024-01-080.0
2024-01-093.0
2024-01-10NaN
Resultant Time Series after applying the interpolation:
DateValue
2024-01-018.000000
2024-01-026.000000
2024-01-034.000000
2024-01-042.000000
2024-01-054.000000
2024-01-062.666667
2024-01-071.333333
2024-01-080.000000
2024-01-093.000000
2024-01-103.000000
廣告