您可以先从这些垃圾箱中形成垃圾箱breaks.time
,然后values.time
使用这些垃圾箱分配类别pd.cut
:
import numpy as np
# intervals to fall into
bins = [-np.inf, *breaks.time, +np.inf]
# distinct labels of 0..N-1
labels = np.arange(len(bins) - 1)
# form a new column in `values` with assigned categories
values["cats"] = pd.cut(values.time, bins=bins, labels=labels)
此时values
看起来像:
>>> values
time value cats
0 0 10312435 0
1 9 45924523 1
2 11 43423434 1
3 20 42343552 2
现在我们可以分组cats
,例如,形成一个数据框列表:
# no need for `cats` column anymore, so we drop it when putting in
frames_list = [frame.drop(columns="cats")
for _, frame in values.groupby("cats")[["time", "value"]]]
我们可以访问框架
>>> frames_list[0]
time value
0 0 10312435
>>> frames_list[1]
time value
1 9 45924523
2 11 43423434
>>> frames_list[2]
time value
3 20 42343552