我有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/