I am trying to create a subset of an astropy table.
I am told I need to use numpy but when I access the table via numpy I don't seem to get a 2d numpy table and am confused.
The table I am trying to create a subset comes from the GAMA Survey
I have tried
import numpy as np
from astropy.table import Table
t = Table.read('GAMA_Data/InputCatA.fits')
ta = np.array(t)
I wish to perform a number of subsets like
t['PETROMAG_I'] > 17.5)
I received some advice that I need to use numpy. I can determine that 'PETROMG_I' is column 16 with
print(t.colnames.index('PETROMAG_I'))
But do not see how to do a subset on either the table or numpy array
print('Source Table : '+str(len(ta)))
print('Shape : '+str(ta.shape))
print(ta)
Gives
Source Table : 960510
Shape : (960510,)
[( 0, 588848900971299297, 145.46972201, 0.65141891, 68987912448, 0, 3.0191224, 20.57372 , 20.748629, 4503874773745664, 0.3678997 , 2.9388053, 1.2919843 , 28.094984, 21.347776, 19.99194 , 19.40016 , 18.74607 , 22.722446, 21.583874, 19.939884, 19.270464, 18.782948, 9843, 756)
( 1, 588848900971299302, 145.47892146, 0.63515137, 68987912448, 0, 3.3755734, 21.073107, 21.055092, 4503874773745664, 0.37012857, 3.619758 , 1.5749472 , 21.076248, 20.432001, 20.08754 , 19.929377, 20.802927, 21.622843, 20.553198, 20.045462, 19.874853, 19.653866, 9843, 756)
Use:
mask = t['PETROMAG_I'] > 17.5 # Makes a boolean selection mask (numpy array)
t_new = t[mask] # Makes a new astropy Table
This will give you an astropy Table with the selected rows. This is documented in an example near the bottom of the section here: https://docs.astropy.org/en/stable/table/access_table.html#accessing-data .
From your printout it look like it was a list of tuples , but probably it is actually a structured Numpy array and 960510 is the number of records (rows) in this table.
Unfortunately, neither field names nor their types can be seen here, but try to execute print(ta.dtypes)
.
Most likely you will see somethig like:
dtype([('Name_1', '<U10'), ('Name_2', '<f8'), ...])
a list of pairs (field name, field type), for details see Numpy documentation.
Then, to access one particular column (in your case PETROMAG_I ), run:
print(ta['PETROMAG_I'])
And to generate your subset, try:
mySubset = ta[ta['PETROMAG_I'] > 17.5]
Another option : Use to_pandas() method of astropy , to convert your Table into a Pandas DataFrame. DataFrames are much more "user friendly" than Numpy arays.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.