遍历熊猫数据帧中的时间序列索引[python]

时间:2019-10-20 20:33:21

标签: python pandas time-series

我是python的初学者。这是我的数据:

       x    y   location    id  label   zone    xx  yy
date                                
2019-10-08 00:00:01.913 456 293 12  2820    0   2   -1.0    0.0
2019-10-08 00:00:01.913 450 234 14  2819    0   3   0.0 -1.0
2019-10-08 00:00:01.913 169 274 0   2700    0   1   1.0 -2.0
2019-10-08 00:00:04.394 449 235 14  2819    0   3   -1.0    1.0
2019-10-08 00:00:04.394 162 282 0   2700    0   1   -7.0    7.0

我有兴趣每小时计算每个区域(有3个区域,分别是0、1,2)中的唯一ID。这是我尝试计算完整csv中唯一ID的数量:

for zone in df.zone.unique():
    print(len(df[df.zone==zone].id.value_counts())))

我正努力每小时一次地这样做。由于date是我的索引,因此我尝试了以下

for i in range(0,22): 
      df1=df['2019-10-08 %d:00:00':'2019-10-08 %d:00:00' %(i,i+1)]
      for zone in df1.zone.unique():
          print(len(df1[df1.zone==zone].id.value_counts()))

也许也不是最好的方法,但这不起作用。

错误: TypeError: not all arguments converted during string formatting

在我的情况下,重新采样不是选项,因为我对计数感兴趣。如果您有更好的方法,我很想知道。

0 个答案:

没有答案