我有一个 numpy 表
<Table length=3>
a b
int64 int64
----- -----
1 3
2 5
4 7
我想将一行转换为 numpy 数组。但是当我尝试时,我最终得到一个没有维度的数组
In: np.array(mytable[0]).shape
Out: ()
如果我这样做
myrow = mytable[0]
myrow[0]
我收到错误
IndexError: too many indices for array
我可以做类似 t[0].values
的事情,它会返回 array([1, 3])
吗?
最佳答案
当您在 Astropy 中从表中分割一行并转换为 ndarray
时,您会得到一个 0D
结构化数组,即 shape 属性为空。对于通用解决方案,numpy
提供了一个 structed_to_unstructed
方法,该方法不仅适用于单行切片。
>>> np.lib.recfunctions.structured_to_unstructured(np.array(t[0]))
array([1, 3])
>>> np.lib.recfunctions.structured_to_unstructured(np.array(t[1:]))
array([[2, 5],
[4, 7]])
关于python - 将天文表中的行转换为 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115763/