我有一个列表列表,其中包含两个表示开始时间点和结束时间点的值。我想计算两点之间的时间范围有多少落入垃圾箱。
箱子在0-300,300-500和500-1200之间。我还想在0-50,50-100,100-150之间加入它们等等。
这个问题类似于Python: Checking to which bin a value belongs,但不同,因为它涉及两个时间点,可以同时落入单独的箱子。
我在下面的代码中创建了一个for循环,它可以工作。但我想知道是否有更快更多的pythonic方式来计算这个,也许使用熊猫或numpy。
import numpy
x = numpy.array([[100, 150],[100, 125],[290, 310],[277, 330],
[300, 400],[480, 510],[500, 600]])
d = {'0-300': [0], '300-500': [0], '500-1200':[0]}
import pandas as pd
df = pd.DataFrame(data=d)
for i in x:
start,end = i[0],i[1]
if start <= 300 and end <= 300: # checks if time ranges falls into only 1st bin
df['0-300'][0] += end - start
elif start <= 300 and end > 300: # checks if time ranges falls into 1st and 2ed bin
df['0-300'][0] += (300 - start)
df['300-500'][0] += (end - 300)
elif start >= 300 and end >= 300 and end <= 500: # checks if time ranges falls into only 2ed bin
df['300-500'][0] += end - start
elif start <= 500 and end > 500: # checks if time ranges falls into 2ed and 3ed bin
df['300-500'][0] += (500 - start)
df['500-1200'][0] += (end - 500)
elif start > 500: # checks if time ranges falls into only 3ed bin
df['500-1200'][0] += end - start
df:
0-300 300-500 500-1200
108 160 110
谢谢阅读
对于通用数量的箱子,这里是一个矢量化的方式利用np.add.at
得到counts
然后np.add.reduceat
获得分箱总结 -
bins = [0, 300, 500, 1200] # Declare bins
id_arr = np.zeros(bins[-1], dtype=int)
np.add.at(id_arr, x[:,0], 1)
np.add.at(id_arr, x[:,1], -1)
c = id_arr.cumsum()
out = np.add.reduceat(c, bins[:-1])
# Present in a dataframe format
col_names = [str(i)+'-' + str(j) for i,j in zip(bins[:-1], bins[1:])]
df_out = pd.DataFrame([out], columns=col_names)
样品输出 -
In [524]: df_out
Out[524]:
0-300 300-500 500-1200
0 108 160 110
这是一种做法
In [1]: counts = np.zeros(1200, dtype=int)
In [2]: for x_lower, x_upper in x: counts[x_lower:x_upper] += 1
In [3]: d['0-300'] = counts[0:300].sum()
In [4]: d['300-500'] = counts[300:500].sum()
In [5]: d['500-1200'] = counts[500:1200].sum()
In [6]: d
Out[6]: {'0-300': 108, '300-500': 160, '500-1200': 110}
但是,为了总结所有箱的结果,最好将这3个步骤包装成for循环。