www.pieronigro.de
www.pieronigro.de

Reputation: 880

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.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

Upvotes: 1

Views: 1716

Answers (3)

www.pieronigro.de
www.pieronigro.de

Reputation: 880

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]

Upvotes: 0

holdenweb
holdenweb

Reputation: 37003

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.

Upvotes: 1

Sudeep Juvekar
Sudeep Juvekar

Reputation: 5068

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

Upvotes: 1

Related Questions