用时间桶对 Python 中的数据进行分组
我想有一定的时间段,然后找到每个时间段的差异来分析。
例如,
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'A-1 A-1 A-1 A-1 A-1 A-1'.split(),
'Date':'23.10.2021 23.10.2021 23.10.2021 23.10.2021 23.10.2021 23.10.2021'.split(),
'Time': '06:05:31 06:11:13 06:19:22 06:25:03 06:33:12 06:44:05'.split(),
'Cumulative': '12 17 19 23 29 38'.split()})
print(df)
我
A Date Time Cumulative
0 A-1 23.10.2021 06:05:31 12
1 A-1 23.10.2021 06:11:13 17
2 A-1 23.10.2021 06:19:22 19
3 A-1 23.10.2021 06:25:03 23
4 A-1 23.10.2021 06:33:12 29
5 A-1 23.10.2021 06:44:05 38
想要的是以 15 分钟为间隔的时间上限,并找出每个时间间隔的差异, 第一步:
A Date Time Cumulative TimeBuckets
0 A-1 23.10.2021 06:05:31 12 06:15:00
1 A-1 23.10.2021 06:11:13 17 06:15:00
2 A-1 23.10.2021 06:19:22 19 06:30:00
3 A-1 23.10.2021 06:25:03 23 06:30:00
4 A-1 23.10.2021 06:33:12 29 06:45:00
5 A-1 23.10.2021 06:44:05 38 06:45:00
在最后阶段作为不同的数据帧,每个时间桶的每个最小值和最大值的差异将被写入:
A Diff TimeBuckets
0 A-1 5 06:15:00
1 A-1 4 06:30:00
2 A-1 8 06:45:00
I would like to have a certain time buckets and then find the difference of each time bucket to analyse.
For example,
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'A-1 A-1 A-1 A-1 A-1 A-1'.split(),
'Date':'23.10.2021 23.10.2021 23.10.2021 23.10.2021 23.10.2021 23.10.2021'.split(),
'Time': '06:05:31 06:11:13 06:19:22 06:25:03 06:33:12 06:44:05'.split(),
'Cumulative': '12 17 19 23 29 38'.split()})
print(df)
out:
A Date Time Cumulative
0 A-1 23.10.2021 06:05:31 12
1 A-1 23.10.2021 06:11:13 17
2 A-1 23.10.2021 06:19:22 19
3 A-1 23.10.2021 06:25:03 23
4 A-1 23.10.2021 06:33:12 29
5 A-1 23.10.2021 06:44:05 38
What I'd like to have is ceiling the hours by 15 mins intervals and find the difference of each,
1st Step:
A Date Time Cumulative TimeBuckets
0 A-1 23.10.2021 06:05:31 12 06:15:00
1 A-1 23.10.2021 06:11:13 17 06:15:00
2 A-1 23.10.2021 06:19:22 19 06:30:00
3 A-1 23.10.2021 06:25:03 23 06:30:00
4 A-1 23.10.2021 06:33:12 29 06:45:00
5 A-1 23.10.2021 06:44:05 38 06:45:00
and in final stage as a different dataframe, difference of each minimum and maximum value for each time bucket would be written:
A Diff TimeBuckets
0 A-1 5 06:15:00
1 A-1 4 06:30:00
2 A-1 8 06:45:00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IIUC,您可以使用
dt.ceil< /code>
和
GroupBy.agg
:输出:
IIUC, you could use
dt.ceil
andGroupBy.agg
:output: