Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算数据帧中每单位时间内的发生率_Python_Pandas_Counting - Fatal编程技术网

Python 计算数据帧中每单位时间内的发生率

Python 计算数据帧中每单位时间内的发生率,python,pandas,counting,Python,Pandas,Counting,我试图计算数据帧中事件的发生率 我在熊猫中有一个数据框,跟踪某个事件的开始和持续时间。因此,一开始,它将沿着以下思路: onset duration label channels end_time 0 1.5 0.1 HFO A1 10 1 2.0 1.0 HFO A2 10 2 3.0 1.0 HFO A3 10 3 5.5 0.1 HFO A

我试图计算数据帧中事件的发生率

我在熊猫中有一个数据框,跟踪某个事件的开始和持续时间。因此,一开始,它将沿着以下思路:

   onset  duration label channels  end_time
0    1.5       0.1   HFO       A1  10
1    2.0       1.0   HFO       A2  10
2    3.0       1.0   HFO       A3  10
3    5.5       0.1   HFO       A1  10
其中
开始时间
持续时间
结束时间
以秒为单位<代码>频道表示我要循环的一组独特的组

期望输出 我想得到这样的东西:

rate_dict = {
  'A1': 0.2,  # rate of 0.2 per second (i.e. 2 occurrences over 10 second time frame)
  'A2': 0.1,  # rate of 0.1 per second
  'A3': 0.1
}
我迄今为止的努力 首先,我根据
频道获得一个组:

for idx, group in df.groupby(['channels']):
然后我把东西转换成日期时间索引

                             onset  duration label channels  end_time
timestamp                                                               
2021-02-10 19:25:19.391130+00:00    1.5       0.1   HFO       A1  10
2021-02-10 19:25:23.391130+00:00    5.5       0.1   HFO       A1  10
接下来,我考虑在开始(0秒)和结束时间(本例中为10秒)内重新编制索引:

问题在于,它无法拾取通道A1在1.5秒和5.5秒时发生的事件。因此,我最终得到的基本上是所有NaN的行,而理想情况下,在我重新采样的这段时间内,我得到的计数是2

期望的泛化 理想情况下,我可以指定其他费率字符串(例如“hr”),它将返回每小时的费率。在这种情况下,这将是:

rate_dict = {
      'A1': 2.0,  # rate of 2 per hr (i.e. 2 occurrences over a 1 hour time frame)
      'A2': 1.0,  # rate of 1 per hr
      'A3': 1.0
    }

Groupby channels,计算发生次数并除以结束时间的平均值。由于每个组的发生次数比组大小小得多,因此在这种情况下,将发生次数定位到结束时间没有什么害处,因为没有太多变化

df.groupby('channels')['end_time'].agg(lambda x: x.count()/x.mean()).to_dict()

首先,我们可以将您的表重新创建为数据帧:

import pandas as pd
d = {'onset': [1.5 ,2.0 ,3.0 ,5.5], 
     'duration': [0.1, 1.0, 1.0, 0.1],
     'label': ['HFO', 'HFO', 'HFO', 'HFO'],
     'channels': ['A1', 'A2', 'A3', 'A1'],
     'end_time': [10.0, 10.0, 10.0, 10.0]}

df = pd.DataFrame(d)
为了直接解决您的问题,根据每秒的发生次数,我们可以计算发生次数并除以平均
结束时间

df.groupby('channels').end_time.agg(lambda x: x.count()/x.mean()).to_dict()
为了概括这一点,让我们创建一个函数
To_freq
,将序列
x
和所需速率作为字符串
rate
作为输入:

def to_freq(x, rate='s'):
    d = {'s':1, 'm': 60, 'h': 60*60, 'd': 60*60*24}
    f = x.count()/x.mean()
    return f/d[rate]
现在,我们的原始代码变成:

df.groupby('channels').end_time.agg(lambda x: to_freq(x)).to_dict()
我们可以找到每小时的发生率,如下所示:

df.groupby('channels').end_time.agg(lambda x: to_freq(x, rate='h')).to_dict()
df.groupby('channels').end_time.agg(lambda x: to_freq(x, rate='h')).to_dict()