How To Access Multiple Fields From A Structured Numpy.array?
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.resha
Solution 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.
Solution 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
Solution 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]
Post a Comment for "How To Access Multiple Fields From A Structured Numpy.array?"