Last active
April 25, 2018 02:35
-
-
Save hongta/d3ed672219c094b3fb00e756ab46c8ec to your computer and use it in GitHub Desktop.
[flatten nested Python dictionsries] #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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