How to access multiple fields from a structured numpy.array?

只愿长相守 提交于 2020-07-09 19:47:36

问题


I came across this difficulty accessing multiple fields (columns)

input:

a = np.array([(1.0, 2,1),(3.0, 4,2),(9, 3,6)], dtype=[('x', float), ('y', float), ('z', float)])
a=np.reshape(a,(a.shape[0],-1))
a

output:

array([[(1.0, 2.0, 1.0)],
       [(3.0, 4.0, 2.0)],
       [(9.0, 3.0, 6.0)]], 
      dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')])

if i want to access the first column i can do:

in: a[:]['x']

out: array([[ 1.], [ 3.], [ 9.]])

but what is the right syntax if i want to access (for example) first an 3rd column? Something like

in: a[:]['x':'z']

obviously does not work


回答1:


Use a list of field names as an index to the array. The result is an array of the same shape, but with only the selected fields in the records (array elements with multiple fields are called records).

import numpy as np
a = np.array([(1.0, 2,1),(3.0, 4,2),(9, 3,6)], dtype=[('x', float), ('y', float), ('z', float)])
print(a)

print(a[['x', 'z']])

You can apply a further level of indexing to the resulting array to select only the required elements, should you choose.




回答2:


a[:][['x', 'z']]

Out[9]: 
array([[(1.0, 1.0)],
   [(3.0, 2.0)],
   [(9.0, 6.0)]], 

Pass the column names as a list




回答3:


Consider having a lot of columns and you do not want to add all items manually - you can do: The column names of a are converted to a list. Afterwards you can access the columns as a list indexwise or itemwise

col_to_ex=list(a.dtype.names)
col_to_ex=col_to_ex[0]+...

or

col_to_ex=list(a.dtype.names).remove('y')

and then you can do:

a[:][col_to_ex]


来源:https://stackoverflow.com/questions/24271828/how-to-access-multiple-fields-from-a-structured-numpy-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!