1

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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure. It took me a while to realize that your array was one-dimensional. But I added that comment in an edit anyway, so that the answer might be more generally useful, particularly to beginners.
1
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

Comments

0

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]

1 Comment

Nice example of extracting the field names dynamically. Why a[:][col_to_ex] rather than a[col_to_ex]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.