python - 按给定列中的值过滤 astropy 表

标签 python filter astropy

我有一个看起来像这样的 astropy 表:

$ print(astro_table)
 id    xcentroid     ycentroid     sharpness        roundness1    ... npix sky      peak          flux            mag       
---- ------------- ------------- -------------- ----------------- ... ---- --- ------------- ------------- -----------------
6603 346.836613078 4089.22051381 0.292000724835   0.0734556783249 ... 49.0 0.0 69302.3984375 245.224909511    -5.97391145737
6177 919.415933548 3859.57301712 0.406784343306   -0.277953216167 ... 49.0 0.0 68524.5234375 220.268294418    -5.85737997245
6602 345.532899395 4088.87923557 0.401278628932   -0.450002792676 ... 49.0 0.0 70189.1953125 210.018583984    -5.80564431499
 ...           ...           ...            ...               ... ...  ... ...           ...           ...               ...
5626 3095.76998822 3522.08198969 0.393572474564   -0.543965319616 ... 49.0 0.0 3037.37036133 1.00374231333 -0.00405558116745
3577  824.59454487 2245.85801066 0.578026446726 -0.00166964746818 ... 49.0 0.0 3150.42285156 1.00347471149 -0.00376608082606
 612 3971.99991783 391.836698859 0.363131852861  -0.0206680542966 ... 49.0 0.0 3087.11572266 1.00319616544 -0.00346465867044
Length = 6603 rows

我想创建一个新表,过滤掉所有 peak 值超过特定 p_max 阈值的星星。

我一直在玩 filter 方法,这是我能想到的:

def not_saturated(table, key_colnames):
    """Filter out saturated stars"""
    if table['peak'] < 60000.:
        return True
    return False

# 'sources' is the Astropy table, generated previously by the code.
# Group the table by 'id' column.
tg = sources.group_by('id')
# Apply filter.
sour_peak_filt = tg.groups.filter(not_saturated)

这行得通,但感觉不必要地令人费解。另外,我想将 p_max 参数传递给 non_saturated() 函数,但我不能,因为它只有两个参数。这迫使我在函数中硬编码一个值 (60000.),我不想这样做。

最佳答案

如我的评论中所述,此答案基于来自 selecting records from a table 的信息.我使用 astropy 发行版中提供的 fits 文件。

>>> from astropy.io import fits
>>> import os
>>> os.chdir("C:\Python34\Lib\site-packages\astropy\io\fits\tests\data")
>>> tableData=fits.open("table.fits")[1].data
>>> print(tableData)
[('NGC1001', 11.1) ('NGC1002', 12.3) ('NGC1003', 15.2)]
>>> tableData.names
['target', 'V_mag']
>>> mask = tableData['V_mag'] < 13.0 
>>> mask
array([ True,  True, False], dtype=bool)
>>> tableData[mask]
FITS_rec([('NGC1001', 11.1), ('NGC1002', 12.3)], 
      dtype=(numpy.record, [('target', 'S20'), ('V_mag', '>f4')]))

关于python - 按给定列中的值过滤 astropy 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511944/

相关文章:

python - 如何将外部文件夹中的 python 模块包含在 docker 镜像中?

c++ - DirectShow 源过滤器

javascript - Ramda - 过滤对象数组 - 柯里化(Currying) fn 参数顺序

python - 如何写入带有小数点后固定数字的 astropy Table 对象值?

python - 如何替换 pandas 数据框中给定列的第 n 行中的值?

python - 在 Python 中使用 smtplib 发送电子邮件

python - Image.fromarray 改变大小

Tomcat 线程池使用模式

python - astropy.io.fits - HIERARCH 关键字不适用于 CONTINUE 卡 : Bug or "Feature" of the FITS standard?

python - 如何用pyfits合并两个表?