Python:检查两个时间点属于哪个bin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python:检查两个时间点属于哪个bin相关的知识,希望对你有一定的参考价值。

我有一个列表列表,其中包含两个表示开始时间点和结束时间点的值。我想计算两点之间的时间范围有多少落入垃圾箱。

箱子在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循环。

以上是关于Python:检查两个时间点属于哪个bin的主要内容,如果未能解决你的问题,请参考以下文章

主题分布:在python中做LDA后如何查看哪个文档属于哪个主题

Python 向 Postman 请求代码片段

如何计算哪个象限属于(坐标)纬度和纬度点

如何检查代码库是用哪个版本的 Python 编写的? [关闭]

在给定多边形坐标的情况下查找点属于哪个多边形的算法

Pandas检查哪个子网IP地址属于