我有一些来自日志文件的数据,并希望按分钟分组:
def gen(date, count=10):
while count > 0:
yield date, "event{}".format(randint(1,9)), "source{}".format(randint(1,3))
count -= 1
date += DateOffset(seconds=randint(40))
df = DataFrame.from_records(list(gen(datetime(2012,1,1,12, 30))), index='Time', columns=['Time', 'Event', 'Source'])
Run Code Online (Sandbox Code Playgroud)
DF:
Event Source
2012-01-01 12:30:00 event3 source1
2012-01-01 12:30:12 event2 source2
2012-01-01 12:30:12 event2 source2
2012-01-01 12:30:29 event6 source1
2012-01-01 12:30:38 event1 source1
2012-01-01 12:31:05 event4 source2
2012-01-01 12:31:38 event4 source1
2012-01-01 12:31:44 event5 source1
2012-01-01 12:31:48 event5 source2
2012-01-01 12:32:23 event6 source1
Run Code Online (Sandbox Code Playgroud)
我试过这些选项:
df.resample('Min') 是太高的水平,想要聚合.df.groupby(date_range(datetime(2012,1,1,12, 30), freq='Min',
periods=4)) 失败,例外.df.groupby(TimeGrouper(freq='Min'))工作正常并返回一个DataFrameGroupBy …