Alexander Kleinhans
Alexander Kleinhans

Reputation: 6248

Get numpy array from an array of tuples

I was using numpy, MySQLbb and scipy and ended up with an array of tuples from a MySQL cursor execution. Then I used np.fromiter. Now I have an array of tuples that looks like this:

>>> A
array([('bob', 0.43), ('dan', 0.24), ('bill', 0.14)
       ('sharen', 0.28), ..., ('zena', 0.24), ('zoe', 0.39)],
      dtype = [('f0', 'S10'), ('f1', '<f4')])

How do I make a numpy array for the first part of each tuple? I tried:

>>> Names = A[:][0]

I also tried:

>>> Names = np.array(A[:][0])

But that didn't work; only gave me the first tuple. I couldn't find any documentation for that specific example.

I want an numpy array like this:

>>> Names 
array[('bob', 'bill', all the other names...
>>> Numbers
array[(0.43, 0.24, etc...

Thanks in advance.

Upvotes: 4

Views: 1137

Answers (1)

shx2
shx2

Reputation: 64318

What you got there is a recarray.

The first field in your array is named 'f0'. You can tell that from the dtype part (A.dtype).

You access it as A['f0'] or A.f0.

Names = A.f0
Numbers = A.f1

Upvotes: 5

Related Questions