子集化时间序列数据
在绘制时间序列数据时,您有时可能只想可视化数据的一个子集。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()