開始使用免費開始

子集化時間序列資料

在繪製時間序列資料時,你有時會只想要視覺化其中一部分。pandas 函式庫提供強大的索引與子集化方法,能讓你從 DataFrame 中擷取特定區段。例如,你可以在 discoveries DataFrame 中,透過下列日期範圍擷取 1950 年到 1960 年之間的所有資料:

subset_data = discoveries['1950-01-01':'1960-01-01']

注意:要用這種方式子集化資料,前提是 DataFrame 的索引包含 datetime 型別的日期。若不符合,pandas 會回傳錯誤訊息。

本練習屬於課程

使用 Python 視覺化時間序列資料

檢視課程

動手互動練習

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

# Select the subset of data between 1945 and 1950
discoveries_subset_1 = discoveries['____':'____']

# Plot the time series in your DataFrame as a blue area chart
ax = discoveries_subset_1.____(color='blue', fontsize=15)

# Show plot
plt.show()
編輯並執行程式碼