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

Time:07-14

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 :)

CodePudding user response:

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
  • Related