開始使用免費開始

2000 年以來紐約市臭氧資料的 360 日滾動中位數與標準差

上個影片也示範了如何使用 .agg() 計算多個滾動統計量,做法與 .groupby() 類似。

我們來更仔細看看紐約市的空氣品質歷史,使用你先前看過的臭氧(Ozone)資料。逐日資料波動很大,因此用較長期的滾動平均能幫助你看出長期趨勢。

你將使用 360 日的滾動視窗,並用 .agg() 對 2000 年以來的每日平均臭氧值計算滾動平均與標準差。

本練習屬於課程

Manipulating Time Series Data in Python

檢視課程

練習說明

我們已經將 pandaspd 匯入,並將 matplotlib.pyplotplt 匯入。

  • 使用 pd.read_csv() 匯入 'ozone.csv',從 'date' 欄位建立 DateTimeIndex(使用 parse_datesindex_col),指定結果為 data,並用 .dropna() 移除遺漏值。
  • 選取 'Ozone' 欄位並建立 360 期的 .rolling() 視窗,套用 .agg() 計算 meanstd,指定為 rolling_stats
  • 使用 .join()datarolling_stats 串接,指定為 stats
  • 使用 subplots 繪製 stats

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import and inspect ozone data here
data = ____

# Calculate the rolling mean and std here
rolling_stats = ____

# Join rolling_stats with ozone data
stats = ____

# Plot stats


編輯並執行程式碼