使用resample基于时间分段的分组操作

In [80]: df[:3]
Out[80]:
    time          value
0   2017-05-20 00:00:00    0
1   2017-05-20 00:01:00    1
2   2017-05-20 00:02:00    2

In [81]: df.set_index('time').resample('5min').count()
Out[81]:
            value
time
2017-05-20 00:00:00    5
2017-05-20 00:05:00    5
2017-05-20 00:10:00    5

set_index('time'):按‘time’进行索引

resample('5min'):以5min为间隔进行采样(分组),分为00:00-00:05,00:05-00:10,00:10-00:15三段

count():对各组进行计数

 

也可以使用pandas.TimeGrouper进行类似的采样操作:

time_key = pd.TimeGrouper('5min')

resampled = (df2.set_index('time').groupby(['key', time_key]).sum())

posted @ 2020-04-09 16:22  zhch0201  阅读(519)  评论(0)    收藏  举报