我有以下格式的数据框。
1 2 3 4
Data
2001-07-30 0.363354 27.428261 14.639130 6.763423
... ... ... ... ...
2015-06-15 0.039085 28.562948 14.000722 8.605911
我希望逐月切片。因此,例如,我想仅选择第5个月的数据并将其存储在新变量中。我找到了类似问题的几个答案。然而,这些并未提供预期的结果。
该领域的问题和答案示例:
我不熟悉Python和Pandas的时间序列分析,所以任何推动正确方向或阅读材料都将受到赞赏。
答案 0 :(得分:2)
datetimeindex
具有要访问的属性month
,您可以使用它来过滤df:
In [132]:
df = pd.DataFrame(index=pd.date_range(dt.datetime(2015,1,1), dt.datetime(2015,5,1)))
df.loc[df.index.month==2]
Out[132]:
Empty DataFrame
Columns: []
Index: [2015-02-01 00:00:00, 2015-02-02 00:00:00, 2015-02-03 00:00:00, 2015-02-04 00:00:00, 2015-02-05 00:00:00, 2015-02-06 00:00:00, 2015-02-07 00:00:00, 2015-02-08 00:00:00, 2015-02-09 00:00:00, 2015-02-10 00:00:00, 2015-02-11 00:00:00, 2015-02-12 00:00:00, 2015-02-13 00:00:00, 2015-02-14 00:00:00, 2015-02-15 00:00:00, 2015-02-16 00:00:00, 2015-02-17 00:00:00, 2015-02-18 00:00:00, 2015-02-19 00:00:00, 2015-02-20 00:00:00, 2015-02-21 00:00:00, 2015-02-22 00:00:00, 2015-02-23 00:00:00, 2015-02-24 00:00:00, 2015-02-25 00:00:00, 2015-02-26 00:00:00, 2015-02-27 00:00:00, 2015-02-28 00:00:00]
月份索引为1
,因此5
为May