python - grouping table elements according to grouping column values -
I have, with the number of photons of an event in each energy channel.
The third column is a group of channels: All the channels marked with -1
are grouped into a single channel, whose original value is the last 1
group value Is on. In this example, all channels from 0 to 39 are grouped in the same channel.
How can I create an array or list of groups with the grouping defined by
/ code> column? In this example, my resultant array will have two elements, one of these channels will be calculated from 0 to 39 and other elements in the 40th channel.
I'm sorry I am not able to give any start codes, but I really do not know how to start. Any suggestions really appreciated.
Edit: The table is part of a FITS file. I read it using the pyfits
:
Import Python Data = Pipes. Open ('. File.fits') chain = data [1]. Data field ('channel') calculation = data [1]. Data field ('calculation') group = data [1]. Data field ('group') data. Closed ()
Print Type (chan)
Return & lt; Type 'numpy.ndarray' & gt;
. The same for the other array.
Try this one,
chain = NP Array ([0,1,2,3,4,5,6,7,8,9]) Calculation = NP. Array ([0., 0., 5., 2., 0, 0., 1., 1., 1., 0]] Group = N.P. Array ([1, -1, -1, -1, -1,1, -1, -1, -1, -1]) indx = np.where (groups == 1) # Indx is a tube with an entry for each dimension of an array group ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Split (calculations, index) # Count is now the list of arrays # [array ([], dtype = float64), array ([0., 0., 5., 0., 0.]), array ([0. , 1., 1., 1., 0.]) # # If I get rid of empty empty array array in list statement = N.P. Array (calculate for [sum (c) c if len (c)> gt]) tchnls = np.split (chan, indx) [1:]
Then Total
will be the sum calculated for each group,
> > altitude ([7., 3.]) > and> Tchnls [array ([0, 1, 2, 3, 4]), array ([5, 6, 7, 8, 9])]
Comments
Post a Comment