6

I have a given array:

array = [(u'Andrew', -3, 3, 100.032) (u'Bob', -4, 4, 103.323) (u'Joe', -5, 5, 154.324)]

that is generated from another process (that I cannot control) of taking a CSV table and it outputs this numpy array. I now need to assign the dtypes of the columns to do further analysis.

How can I do this?

Thank you

1
  • what is this question -1? Commented Aug 27, 2014 at 9:48

2 Answers 2

8

Is this what you need ?

new_array = np.array(array, dtype = [("name", object), 
                                     ("N1", int), 
                                     ("N2", int),
                                     ("N3", float)])

where name and N1-3 are column names I gave.

It gives :

array([(u'Andrew', -3, 3, 100.032), (u'Bob', -4, 4, 103.323),
       (u'Joe', -5, 5, 154.324)], 
      dtype=[('name', 'O'), ('N1', '<i8'), ('N2', '<i8'), ('N3', '<f8')])

You can sort on "N1" for instance :

new_array.sort(order="N1")
new_array
array([(u'Joe', -5, 5, 154.324), (u'Bob', -4, 4, 103.323),
       (u'Andrew', -3, 3, 100.032)], 
      dtype=[('name', 'O'), ('N1', '<i8'), ('N2', '<i8'), ('N3', '<f8')])

Hope this helps.

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

Comments

3
recarr = np.rec.fromrecords(array)

Optionally set field names:

recarr = np.rec.fromrecords(array, names="name, idata, idata2, fdata")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.