astrofrog
astrofrog

Reputation: 34161

List of tuples to Numpy recarray

Given a list of tuples, where each tuple represents a row in a table, e.g.

tab = [('a',1),('b',2)]

Is there an easy way to convert this to a record array? I tried

np.recarray(tab,dtype=[('name',str),('value',int)])

which doesn't seem to work.

Upvotes: 2

Views: 1744

Answers (1)

John La Rooy
John La Rooy

Reputation: 304375

try

np.rec.fromrecords(tab)

rec.array([('a', 1), ('b', 2)], 
          dtype=[('f0', '|S1'), ('f1', '<i4')])

Upvotes: 4

Related Questions