使用 interpolate 方法進行插補
時間序列資料會隨時間呈現上升或下降的趨勢。對這類資料,用 forward fill 或 backward fill 這種把缺值平填的方式並不合適。更適切的作法是使用線性或二次插補等方法,讓填補值依趨勢逐步增加或減少。
在這個練習中,你將在 airquality DataFrame 上使用 .interpolate() 方法。你會嘗試 linear、quadratic 與 nearest 三種方法。你也可以在這裡找到更完整的插值策略清單:here。
本練習屬於課程
在 Python 中處理遺漏值
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print prior to interpolation
print(airquality[30:40])
# Interpolate the NaNs linearly
airquality.interpolate(___, inplace=True)
# Print after interpolation
print(airquality[30:40])