Question

What methods are available to convert a loosely coupled list of dictionaries to a np.recarray (where import numpy as np)?

I looked around here on SO, but namely saw things where the data was already well-structured.

I've prototyped a simple method here: dict_list_to_recarray.py

Thank you!

Was it helpful?

Solution

@DSM beat me to it when I am preparing a full answer. But yes, it is sort-of already there. Only that you have to use Pandas (imported as pd) to convert it to a DataFrame and then convert it to a recarray

In [8]:

print pd.DataFrame(raw)
   age             extra has_money   name
0   45               NaN      True  Billy
1   32  Something wicked     False  Tommy
2   31               NaN      True     Al
In [9]:

pd.DataFrame(raw).to_records()
Out[9]:
rec.array([(0, 45, nan, True, 'Billy'),
       (1, 32, 'Something wicked', False, 'Tommy'),
       (2, 31, nan, True, 'Al')], 
      dtype=[('index', '<i8'), ('age', '<i8'), ('extra', 'O'), ('has_money', '?'), ('name', 'O')])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top