简体   繁体   中英

Is there a way in Python to extract values from a list of nested dictionaries?

I have a list of nested dictionaries looking like this one:

list_1 = [{'one':{'two':{'three':{'a':10,
                                 'b':20,
                                 'c':30,
                                 'd':[1,2,3,4]}}}}]

I would like to get a dataframe that looks like the following:

key values
d 1
d 2
d 3
d 4

Thanks for your help, munch needed and appreciated :)

import pandas as pd

list_1 = [{"one": {"two": {"three": {"a": 10,
                                     "b": 20,
                                     "c": 30,
                                     "d": [1, 2, 3, 4]}}}}]


df = pd.DataFrame({"key": "d", 
                   "value": list_1[0]["one"]["two"]["three"]["d"]})

print(df)

>>>   key  values
>>> 0   d      1
>>> 1   d      2
>>> 2   d      3
>>> 3   d      4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM