gpt4 book ai didi

python - 15 分钟内从 pandas Dataframe 到 dataframe 时间序列 Bins by sum rows

转载 作者:行者123 更新时间:2023-12-01 08:22:13 25 4
gpt4 key购买 nike

例如,我有一个像这样的数据框:

    ID  Arrival_time
....
0 22 2019-01-01 05:34:10
1 23 2018-01-01 05:36:18
2 24 2018-01-01 05:44:24
3 25 2018-01-01 06:10:26
4 26 2018-01-01 06:08:28
5 27 2018-01-01 06:22:29
....

我需要一个新的数据帧,其中包含 15 分钟时间段的时间序列作为索引,以及包含这 15 分钟内计数的 ARRIVAL_TIME 条目的列。

对于上述内容,我期望类似:

                   COUNTED_ARRIVALS
....
2019-01-01 05:30 3
2019-01-01 05:45 0
2019-01-01 06:00 2
2019-01-01 06:15 1
2019-01-01 06:30 0
2019-01-01 06:45 0
2019-01-01 07:00 0
....

如何在 pandas 中实现此功能?

最佳答案

您可以尝试使用pd.cut来获取时间范围内的值的容器

df

1 time
0 21 2018-01-01 05:34:10
1 23 2018-01-01 05:36:18
2 24 2018-01-01 05:44:24
3 25 2018-01-01 06:10:26
4 26 2018-01-01 06:08:28
5 27 2018-01-01 06:22:29


bins = pd.date_range(start=df['time'].min().floor('15Min'),end=df['time'].max().floor('15Min'),freq='15Min')
df.groupby(pd.cut(df['time'],bins)).count()

输出:

                                           1    time
time
(2018-01-01 05:30:00, 2018-01-01 05:45:00] 3 3
(2018-01-01 05:45:00, 2018-01-01 06:00:00] 0 0
(2018-01-01 06:00:00, 2018-01-01 06:15:00] 2 2

关于python - 15 分钟内从 pandas Dataframe 到 dataframe 时间序列 Bins by sum rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553403/

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