gpt4 book ai didi

python - Pandas 将基于日期时间类型的数据框分组到忽略日期部分的不同时期

转载 作者:行者123 更新时间:2023-11-28 22:33:39 24 4
gpt4 key购买 nike

我想根据可变时间间隔将行分组。但是,在做分组的时候,我想忽略日期部分,只根据时间日期分组。

假设我想每 5 分钟分组一次。

       timestampe            val
0 2016-08-11 11:03:00 0.1
1 2016-08-13 11:06:00 0.3
2 2016-08-09 11:04:00 0.5
3 2016-08-05 11:35:00 0.7
4 2016-08-19 11:09:00 0.8
5 2016-08-21 12:37:00 0.9

into

timestampe val
0 2016-08-11 11:03:00 0.1
2 2016-08-09 11:04:00 0.5

timestampe val
1 2016-08-13 11:06:00 0.3
4 2016-08-19 11:09:00 0.8

timestampe val
3 2016-08-05 11:35:00 0.7
timestampe val
5 2016-08-21 12:37:00 0.9

请注意,只要时间在相同的 5 分钟间隔内,行就会分组,而不管日期如何。

最佳答案

这是假设您将一天分成 5 分钟的窗口

df.groupby(df.timestampe.dt.hour.mul(60) \
.add(df.timestampe.dt.minute) // 5) \
.apply(pd.DataFrame.reset_index)

enter image description here


for name, group in df.groupby(df.timestampe.dt.hour.mul(60).add(df.timestampe.dt.minute) // 5):
print name
print group
print

132
timestampe val
0 2016-08-11 11:03:00 0.1
2 2016-08-09 11:04:00 0.5

133
timestampe val
1 2016-08-13 11:06:00 0.3
4 2016-08-19 11:09:00 0.8

139
timestampe val
3 2016-08-05 11:35:00 0.7

151
timestampe val
5 2016-08-21 12:37:00 0.9

关于python - Pandas 将基于日期时间类型的数据框分组到忽略日期部分的不同时期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com