
上QQ阅读APP看书,第一时间看更新
004 使用月份设置DataFrame的行标签
此案例主要演示了使用period_range()函数根据起止月份设置DataFrame的行标签。当在Jupyter Notebook中运行此案例代码之后,将在DataFrame中把行标签从0到4调整为从2021-01到2021-05,效果分别如图004-1和图004-2所示。

图004-1

图004-2
主要代码如下。

在上面这段代码中,df.index=pd.period_range(start='2021-01',end='2021-05',freq='M')表示在df中将行标签调整为从2021-01到2021-05,参数start='2021-01'表示起始月份,参数end='2021-05'表示结束月份,参数freq='M'表示以月份为单位。
此案例的主要源文件是MyCode\H717\H717.ipynb。