pandas 如何将行分组为不同的时间段? [英] Panda how to groupby rows into different time buckets?
问题描述
我有一个带有日期时间类型列的数据框,称为时间戳,我想根据时间戳将时间部分分为几个数据框,每个数据框都包含按x分钟数取值的行,其中x是变量.
I have a dataframe with a datetime type column called timestamp, I want to split the dataframe into several dataframes based on timestamp the time part, each dataframe contains rows that value by its value modulo x minutes, where x is a variable.
请注意,e
和f
的顺序不是原始的.用10分钟为模,我希望所有以3
结尾的时间在一起,所有以1
结尾的时间在一起,依此类推.
Notice that e
and f
are not in original order. With modulo 10 minutes, I want all times that end in 3
together, all times that end in 1
together, so on and so forth.
当x = 10时分组
timestampe text
0 2016-08-11 12:01:00 a
1 2016-08-13 11:11:00 b
2 2016-08-09 11:13:00 c
3 2016-08-05 11:33:00 d
4 2016-08-19 11:27:00 e
5 2016-08-21 11:43:00 f
进入
timestampe text
0 2016-08-11 12:01:00 a
1 2016-08-13 11:11:00 b
0 2016-08-09 11:13:00 c
1 2016-08-05 11:33:00 d
2 2016-08-21 11:43:00 f
0 2016-08-19 11:27:00 e
推荐答案
您的主要工具将是df.timestampe.dt.minute % 10
和groupby
.
为了方便说明,我使用了apply(pd.DataFrame.reset_index)
Your main tools will be df.timestampe.dt.minute % 10
and groupby
.
I used an apply(pd.DataFrame.reset_index)
just as a convenience to illustrate
df.groupby(df.timestampe.dt.minute % 10).apply(pd.DataFrame.reset_index)
仅使用groupby
也是有利的
for name, group in df.groupby(df.timestampe.dt.minute % 10):
print
print(name)
print(group)
1
timestampe text
0 2016-08-11 12:01:00 a
1 2016-08-13 11:11:00 b
3
timestampe text
2 2016-08-09 11:13:00 c
3 2016-08-05 11:33:00 d
5 2016-08-21 11:43:00 f
7
timestampe text
4 2016-08-19 11:27:00 e
这篇关于 pandas 如何将行分组为不同的时间段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!