python - 将天文表中的行转换为 numpy 数组

标签 python arrays numpy astropy

我有一个 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/

相关文章:

c++ - 数组作为类的私有(private)成员

python - 如何将 Pandas 数据框转换为带有列名的 numpy 数组

python - allclose() 如何工作?

python - NumPy:类型错误:reshape() 获得意外的关键字参数 'order'

Python将长字符串转换为不带单引号的列表

python - 我如何重新格式化日期

python - 使用 skimage view_as_windows 制作图像补丁和重建补丁

arrays - 算法 - 在未排序的数组中找到具有最小距离的两个数字的最大总和

javascript - 实现 Knockout.js 破坏性 foreach 循环

Python Find max in dataframe 列以循环查找所有值