Skip to content

Instantly share code, notes, and snippets.

@hongta
Last active April 25, 2018 02:35
Show Gist options
  • Select an option

  • Save hongta/d3ed672219c094b3fb00e756ab46c8ec to your computer and use it in GitHub Desktop.

Select an option

Save hongta/d3ed672219c094b3fb00e756ab46c8ec to your computer and use it in GitHub Desktop.
[flatten nested Python dictionsries] #python
import collections
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
# >>> flatten({'a': 1, 'c': {'a': 2, 'b': {'x': 5, 'y' : 10}}, 'd': [1, 2, 3]})
# {'a': 1, 'c_a': 2, 'c_b_x': 5, 'd': [1, 2, 3], 'c_b_y': 10}
# use pandas' json_normalize function
from pandas.io.json import json_normalize
# create pandas' DataFrame
df = json_normalize({'a': 1, 'c': {'a': 2, 'b': {'x': 5, 'y' : 10}}, 'd': [1, 2, 3]})
# df.head()
# a c.a c.b.x c.b.y d
#0 1 2 5 10 [1, 2, 3]
df.to_dict("records")
# [{'a': 1, 'c.a': 2, 'c.b.x': 5, 'c.b.y': 10, 'd': [1, 2, 3]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment