使用索引数组建立一个Astropy表

问题描述 投票:0回答:1

我有两个目录我想打造成为Astropy表,我似乎无法使它工作。两个阵列具有不同的长度和我有一个第三阵列包含索引我需要使用建表。这两个目录如下所示:

c1['x'] = [11.7, 13.8, 19.2, 15.0]
c2['x'] = [12.0, 13.6, 14.5]
idx = [0, 2]

我需要创建一个表只包括在两个目录的第一和第三项。我努力了:

from astropy.table import Table
t = Table([idx, c1, cat2], names=('idx', 'x', 'x'))   
t

这给了我一个错误:“数据不一致列长度:{4,3}”我觉得我可能要对这个奇怪的方式。

python-3.x datatable astropy
1个回答
1
投票

使用numpy的阵列,而不是Python列表来表示你的数据,并创建idx前申请行选择指数Table

>>> import numpy as np
>>> from astropy.table import Table
>>> c1x = np.array([11.7, 13.8, 19.2, 15.0])
>>> c2x = np.array([12.0, 13.6, 14.5])
>>> idx = np.array([0, 2])
>>> c1x[idx]
array([11.7, 19.2])
>>> c2x[idx]
array([12. , 14.5])
>>> table = Table()
>>> table['c1x'] = c1x[idx]
>>> table['c2x'] = c2x[idx]
>>> table
<Table length=2>
  c1x     c2x  
float64 float64
------- -------
   11.7    12.0
   19.2    14.5

Astropy表内部存储的数据作为numpy的数组,所以如果你已经有两个表c1c2你可以这样做:

>>> c1 = Table()
>>> c1['x'] = [11.7, 13.8, 19.2, 15.0]
>>> c2 = Table()
>>> c2['x'] = [12.0, 13.6, 14.5]
>>> idx = [0, 2]
>>> t = Table()
>>> t['c1x'] = c1['x'][idx]
>>> t['c2x'] = c2['x'][idx]
>>> t
<Table length=2>
  c1x     c2x  
float64 float64
------- -------
   11.7    12.0
   19.2    14.5

有许多不同的方法来创建表,从中选择行和列,或将数据从两个表合并成一个新表。例如。你的情况,你可能想申请idx选择每个表c1c2中的相关行,然后再加入两个表(即现在有同样的行数)。见Astropy Table docs

© www.soinside.com 2019 - 2024. All rights reserved.