Skip to content

Instantly share code, notes, and snippets.

@eacousineau
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eacousineau/11090454 to your computer and use it in GitHub Desktop.
Save eacousineau/11090454 to your computer and use it in GitHub Desktop.
dict_list_to_recarray.py
#!/usr/bin/python
import numpy as np
def dict_list_to_recarray(items):
"""
@brief Take a loose list of dictionaries, and turn them into a recarray with
fields defined as 'object' for clarity
@param items Iterable collection of dictionaries
@author Eric Cousineau <eacousineau@gmail.com>
@note xref: http://docs.scipy.org/doc/numpy/reference/generated/numpy.core.records.fromrecords.html
"""
# Collect the unique fields
fields_raw = []
for item in items:
fields_raw += item.keys()
fields = set(fields_raw)
# Get value sets, replacing empty fields with 'None'
rec_list = []
for item in items:
values = [item.get(field, None) for field in fields]
rec_list.append(values)
# Construct the np.recarray
return np.core.records.fromrecords(rec_list, names=','.join(fields))
if __name__ == '__main__':
raw = [
{'name': 'Billy', 'has_money': True, 'age': 45},
{'name': 'Tommy', 'has_money': False, 'age': 32, 'extra': 'Something wicked'},
{'name': 'Al', 'has_money': True, 'age': 31}
]
rec = dict_list_to_recarray(raw)
print "Names: {}".format(', '.join(rec.name))
print "Mean Age: {}".format(np.mean(rec.age))
print "Money Havers: {} / {}".format(np.sum(rec.has_money), len(rec))
print "Extras: {}".format(', '.join(map(str, rec.extra)))
"""
Names: Billy, Tommy, Al
Mean Age: 36.0
Money Havers: 2 / 3
Extras: None, Something wicked, None
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment