我有一个带有日期列的数据框:
我想根据时间将 DF 分为 3 个 DF:
没有循环怎么实现?
我可以通过在每个时间戳上使用循环来实现此目的,但速度非常慢。
您可以减去标准化日期以获得时间增量,然后
cut
和groupby
:
bins = ['0', '12h', '18h', '24h']
labels = [f'{bins[i]}-{bins[i+1]}' for i in range(len(bins)-1)]
out = dict(list(df.groupby(pd.cut(df['Date'].sub(df['Date'].dt.normalize()),
[pd.Timedelta(x) for x in bins],
labels=labels, include_lowest=True))))
输出:
{'0-12h': Date
0 2023-01-01 00:00:00
1 2023-01-01 00:10:00
2 2023-01-01 00:20:00
3 2023-01-01 00:30:00
4 2023-01-01 00:40:00
... ...
1221 2023-01-09 11:30:00
1222 2023-01-09 11:40:00
1223 2023-01-09 11:50:00
1224 2023-01-09 12:00:00
1296 2023-01-10 00:00:00
[658 rows x 1 columns],
'12h-18h': Date
73 2023-01-01 12:10:00
74 2023-01-01 12:20:00
75 2023-01-01 12:30:00
76 2023-01-01 12:40:00
77 2023-01-01 12:50:00
... ...
1256 2023-01-09 17:20:00
1257 2023-01-09 17:30:00
1258 2023-01-09 17:40:00
1259 2023-01-09 17:50:00
1260 2023-01-09 18:00:00
[324 rows x 1 columns],
'18h-24h': Date
109 2023-01-01 18:10:00
110 2023-01-01 18:20:00
111 2023-01-01 18:30:00
112 2023-01-01 18:40:00
113 2023-01-01 18:50:00
... ...
1291 2023-01-09 23:10:00
1292 2023-01-09 23:20:00
1293 2023-01-09 23:30:00
1294 2023-01-09 23:40:00
1295 2023-01-09 23:50:00
[315 rows x 1 columns],
}