Skip to main content
 首页 » 编程设计

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

2024年10月25日9wayfarer

我有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])]