python - 根据分组列值对表元素进行分组

标签 python numpy scipy

我有this table ,每个能量 channel 中事件的光子计数数。

第三列是 channel 的分组:所有标记为-1的 channel 被分组为一个 channel ,其原点位于上一个 1分组值。也就是说,在本例中,从 0 到 39 的所有 channel 都被分组为一个 channel 。

如何使用 group 定义的分组创建计数数组或列表柱子? 在此示例中,我的结果数组将包含两个元素,一个元素包含 channel 0 到 39 的计数总和,另一个元素包含第 40 个 channel 中的计数。

很抱歉我无法提供任何起始代码,但我真的不知道如何开始。任何建议都非常感谢。

编辑:该表是 FITS 文件的一部分。我通过使用 pyfits 阅读它:

import pyfits
data = pyfits.open('./file.fits')
chan    = data[1].data.field('channel')
counts    = data[1].data.field('counts')
groups    = data[1].data.field('grouping')
data.close()

print type(chan)返回<type 'numpy.ndarray'> 。其他数组也是如此。

最佳答案

尝试一下,

chan = np.array( [0,1,2,3,4,5,6,7,8,9] )
counts = np.array( [0.,0.,5.,2.,0.,0.,1.,1.,1.,0.] )
groups = np.array( [1,-1,-1,-1,-1,1,-1,-1,-1,-1] )

indx = np.where( groups==1 )
# indx is a tuple with one entry for each dimension of the array groups
# in the next statement I just grab the first (and only) element of the tuple 
indx = indx[0]  

# next we split the array based on those indices
counts = np.split( counts, indx )
# counts is now a list of arrays 
# [array([], dtype=float64), array([ 0.,  0.,  5.,  0.,  0.]),array([ 0.,  1.,  1.,  1.,  0.])]
# I use the if statement in the list comprehension to get rid of the first empty array

totals = np.array( [sum(c) for c in counts if len(c)>0] )
tchnls = np.split( chan, indx )[1:]

然后 totals 将是每组的计数总和,

>>> totals
array([ 7.,  3.])

tchnls 将是为每个组做出贡献的 channel ,

>>> tchnls
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]

关于python - 根据分组列值对表元素进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25072134/

相关文章:

python - Scipy.optimize.curve_fit 不符合余弦幂律

python - 如何将 iPython HTML 类发送到 .html 文件?

python - 类型错误 : 'module' object is not callable API

python - 下载网站中的所有文件

python - 将 numpy 列表转换为列向量

python - 使用 scipy.interpolate.splrep 函数

python - 将字符串列表提供给 sql 查询 [Python]

python - 如何利用所有核心来加速基于numpy 3D数组的仿真程序?

python - 数学方程的 Numpy 向量化

python - 使用无循环的numpy在二维数组中堆叠每2个数组