Reputation: 3533
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!
Upvotes: 1
Views: 1459
Reputation: 54380
@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')])
Upvotes: 3