我想我最好通过一个例子来解释我想要实现的目标。假设我有这个数据框:
time
0 2013-01-01 12:56:00
1 2013-01-01 12:00:12
2 2013-01-01 10:34:28
3 2013-01-01 09:34:54
4 2013-01-01 08:34:55
5 2013-01-01 16:35:19
6 2013-01-01 16:35:30
我想,给定一个时间间隔 T,计算每行在该时间间隔内“打开”了多少个寄存器。例如,考虑到 T = 2 小时,这将是输出:
time count
0 2013-01-01 12:56:00 1 # 12:56-2 = 10:56 -> 1 register between [10:56, 12:56)
1 2013-01-01 12:00:12 1
2 2013-01-01 10:34:28 2 # 10:34:28-2 = 8:34:28 -> 2 registers between [8:34:28, 10:34:28)
3 2013-01-01 09:34:54 1
4 2013-01-01 08:34:55 0
5 2013-01-01 16:35:19 0
6 2013-01-01 16:35:30 1
我想知道如何使用 pandas 获得这个结果。例如,如果我只考虑 dt.hour 访问器(accessor),对于 T 等于 1,我可以创建每小时的列计数,然后将其移动 1,对 count[i] + count[i-1]
的结果求和。 。但我不知道是否可以将其概括为所需的输出。
最佳答案
这里的想法是将所有寄存器开放时间标记为+1,将所有寄存器关闭时间标记为-1。然后按时间排序并对 +/- 1 值执行累积求和,以获得给定时间打开的计数。
# initialize interval start times as 1, end times as -1
start_times= df.assign(time=df['time'] - pd.Timedelta(hours=2), count=1)
all_times = start_times.append(df.assign(count=-1), ignore_index=True)
# sort by time and perform a cumulative sum get the count of overlaps at a given time
# (subtract 1 since you don't want to include the current value in the overlap)
all_times = all_times.sort_values(by='time')
all_times['count'] = all_times['count'].cumsum() - 1
# reassign to the original dataframe, keeping only the original times
df['count'] = all_times['count']
结果输出:
time count
0 2013-01-01 12:56:00 1
1 2013-01-01 12:00:12 1
2 2013-01-01 10:34:28 2
3 2013-01-01 09:34:54 1
4 2013-01-01 08:34:55 0
5 2013-01-01 16:35:19 0
6 2013-01-01 16:35:30 1
关于python - 统计间隔内的寄存器数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49240140/