昨天我问了这个问题(How to sort through a list of observable coordinates?)关于排序坐标列表以删除低于阈值的某些值,我从@MSeifert得到了一个很好的答案,但我有一个表格,其中这些坐标值匹配与目标的其他属性(例如视在幅度和Alt / Az坐标),所以我现在要求的是一种在我的astropy.table
而不是astropy.coordinates.SkyCoord
列表中执行此屏蔽技术的方法上一个问题。
让我概述一下问题:
我获得了我最初的RA& Dec使用此行进行坐标:
radecs = astropy.coordinates.SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad)
然后我将其转换为Table
:
x=Table([radecs.ra,radecs.dec,altaz.alt,altaz.az,app_mag], names=('RA',
'Dec','Altitude', 'Azimuth','Apparent Magnitude'))
忽略幅度列,它看起来像这样:
我想删除与偏差小于-10度的行相对应的行,类似于我在第一个问题中要求执行此操作的行,但也要从表中删除整行数据,而不仅仅是'radecs'的价值观。
我在网上发现了this问题,这对我说,从表中删除行的功能不存在(至少在2013年6月),但我认为应该有一个相当简单的解决方案来解决我的问题。
答案 0 :(得分:1)
对不起,我请你再提一个问题。我以为你可能已经将坐标保存在一列而不是不同的列中。
如果它们位于不同的列中,它的工作方式与其他答案完全相同(但您必须稍微更改索引),因为您需要索引列而不是属性:
x=Table(...)
x1 = x[x['Dec'] > -10*u.degree] # Remove everything below -10degree declination
x2 = x[x['Apparent Magnitude'] < 20] # Remove every star with a magnitude above 20
因此x['Apparent Magnitude']
会为您提供'Apparent Magnitude'
列,依此类推。
此外:您还可以指定更复杂的条件:
magnitude_below_20 = x['Apparent Magnitude'] < 20
dec_above_m10 = x['Dec'] > -10*u.degree
# Get every row that satisfies both conditions:
x_from_comb_condition = x[magnitude_below_20 & dec_above_m10]
# or using the numpy-ufunc (gives the same result):
x_from_comb_condition = x[np.logical_and(magnitude_below_20, dec_above_m10)]
或如果一个条件适用(|
或np.logical_or
)或条件不适用(~
或np.logical_not
)
例如:
from astropy.coordinates import SkyCoord
from astropy.table import Table
import astropy.units as u
import numpy as np
phi = np.linspace(0,2*np.pi,20)
theta = np.linspace(0, np.pi, 20)
radecs = SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad)
app_mag = np.random.uniform(15,25, 20)
x=Table([radecs.ra,radecs.dec,app_mag], names=('RA', 'Dec','Apparent Magnitude'))
x[x['Dec'] > -10*u.degree]
给了我:
RA Dec Apparent Magnitude
deg deg
float64 float64 float64
0.0 90.0 20.1080708665
18.9473684211 80.5263157895 22.7223534546
37.8947368421 71.0526315789 19.4416167208
56.8421052632 61.5789473684 20.7207435685
75.7894736842 52.1052631579 19.9318711443
94.7368421053 42.6315789474 23.8544483535
113.684210526 33.1578947368 15.8981196334
132.631578947 23.6842105263 24.2866475431
151.578947368 14.2105263158 15.9503148326
170.526315789 4.73684210526 16.5505303858
189.473684211 -4.73684210526 24.194771397
而完整的Table
是:
RA Dec Apparent Magnitude
deg deg
float64 float64 float64
0.0 90.0 20.1080708665
18.9473684211 80.5263157895 22.7223534546
37.8947368421 71.0526315789 19.4416167208
56.8421052632 61.5789473684 20.7207435685
75.7894736842 52.1052631579 19.9318711443
94.7368421053 42.6315789474 23.8544483535
113.684210526 33.1578947368 15.8981196334
132.631578947 23.6842105263 24.2866475431
151.578947368 14.2105263158 15.9503148326
170.526315789 4.73684210526 16.5505303858
189.473684211 -4.73684210526 24.194771397
208.421052632 -14.2105263158 15.4721094564
227.368421053 -23.6842105263 20.6082525987
246.315789474 -33.1578947368 21.9730819638
265.263157895 -42.6315789474 17.3627571053
284.210526316 -52.1052631579 22.7065806097
303.157894737 -61.5789473684 23.7244993197
322.105263158 -71.0526315789 19.7676029836
341.052631579 -80.5263157895 19.2663871267
0.0 -90.0 19.5025214878
所以这只保留满足条件的行,并“忽略”所有其他行。
可以手动删除行(如果知道行号):
x.remove_row(0) # Removes the first row
x.remove_row(-1) # Removes the last row
但是,如果您因条件而要丢弃行,通常会编制索引。 remove_row
更多是手动删除一行/两行。也许是因为他们是误报或别的......
他们就地工作。所以永远不要x = x.remove_row(-1)
,因为x
将是None
。