2

Say I have the following array:

a = array([(1L, 2.0, 'buckle_my_shoe'), (3L, 4.0, 'margery_door')], 
dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', 'S14')])

How do I access a column?

I can access a row using this syntax:

a[0][:]

but get an error when I try to access a column in the same way.

a[:][0]

Note. This is not a dupe of "How to access the ith column of a NumPy multidimensional array?" since I am using an array of different types.

Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170

1 Answers1

2
In [33]: a['f0']
Out[33]: array([1, 3], dtype=int64)

In [34]: a['f1']
Out[34]: array([ 2.,  4.])

In [35]: a['f2']
Out[35]: 
array(['buckle_my_shoe', 'margery_door'], 
      dtype='|S14')

Here, f0, f1 and f2 are the field names from your array's dtype.

For more information, see Structured Arrays.

NPE
  • 486,780
  • 108
  • 951
  • 1,012