1

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!

eacousineau
  • 3,457
  • 3
  • 34
  • 37
  • 1
    In my experience when people are using `recarrays` they'd usually be better off using [`pandas`](http://pandas.pydata.org) `DataFrames`; you could simply write `df =pd.DataFrame(raw)`. And then you could get the equivalent recarray using `df.to_records()` if you wanted. (`pandas` uses `NaN` to signify missing values where you're using `None`, but `.fillna()` could change that.) – DSM Apr 19 '14 at 17:20

1 Answers1

3

@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')])
CT Zhu
  • 52,648
  • 17
  • 120
  • 133