首页 > 解决方案 > 如何在 Pandas 中对日期时间列进行分组?

问题描述

我想将我的日期时间列数据分为 4 个组,命名为: 12AM-06 AM,06 AM-12PM,12PM-18PM,18PM-12AM 。我怎样才能做到这一点?

          Time_Interval
0         00:00:00
1         00:00:00
2         06:00:00
3         12:00:00
4         12:00:00
            ...   
209526    18:00:00
209527    00:00:00
209528    06:00:00
209529    12:00:00
209530    12:00:00

起初我试图将日期时间转换为对象,但得到了这个错误:只能使用带有类似日期时间值的 .dt 访问器这是我的代码:

df['Time_Interval'] = df['Time_Interval'].dt.strftime('%H:%M:%S')

什么是最好的解决方案?

标签: pythonpandasdatetime

解决方案


推荐阅读