我的pd如下:
df=
CreationDate
0 2008-11-04 13:21:39
1 2008-11-24 23:50:29
2 2009-05-18 07:46:48
3 2009-09-22 06:03:34
4 2009-11-07 07:28:21
5 2009-12-08 14:29:56
6 2010-01-12 06:42:00
7 2010-05-20 17:56:01
8 2010-06-05 19:27:02
9 2010-07-16 19:52:22
10 2010-07-25 16:27:13
11 2010-07-25 21:59:56
CreationDate datetime64[ns]
dtype: object
现在我需要在特定时间内选择和计数项目,例如,我需要这些项目并计算它们的形式' 2008-11'至2009-12'。如何选择和统计?感谢。
答案 0 :(得分:1)
您需要先将CreationDate
设为索引,然后使用切片'2008-11':'2009-12'
进行选择。
print(df)
CreationDate A
0 2008-11-04 13:21:39 1.7641
1 2008-11-24 23:50:29 0.4002
2 2009-05-18 07:46:48 0.9787
3 2009-09-22 06:03:34 2.2409
4 2009-11-07 07:28:21 1.8676
5 2009-12-08 14:29:56 -0.9773
6 2010-01-12 06:42:00 0.9501
7 2010-05-20 17:56:01 -0.1514
8 2010-06-05 19:27:02 -0.1032
9 2010-07-16 19:52:22 0.4106
10 2010-07-25 16:27:13 0.1440
11 2010-07-25 21:59:56 1.4543
# processing
# ==========================
df.set_index('CreationDate')['2008-11':'2009-12']
A
CreationDate
2008-11-04 13:21:39 1.7641
2008-11-24 23:50:29 0.4002
2009-05-18 07:46:48 0.9787
2009-09-22 06:03:34 2.2409
2009-11-07 07:28:21 1.8676
2009-12-08 14:29:56 -0.9773