简体   繁体   中英

Flattening/normalizing deeply nested objects with unique keys

What is the ideal way to parse a JSON and set the deeply nested attributes as columns.

{"liked_food":
  {"A":
     {"-cheesecake":{"name": "cheesecake", "calories":500,"ingredients":"sugar, eggs, cheese, crackers","serving_size":7},
     "-donut":{"name": "donut", "calories":150,"ingredients":"sugar, eggs, yeast","serving_size":1.5}},
  "B":
     {"-cheesecake":{"name": "cheesecake", "calories":500,"ingredients":"sugar, eggs, cheese, crackers","serving_size":7}}
  }
}

Goal: Get the above JSON snippet formatted similar to the below dataframe:

name calories ingredients serving_size
A cheesecake 500 sugar, eggs, cheese, crackers 7
A donut 150 sugar, eggs, yeast 1.5
B cheesecake 500 sugar, eggs, cheese, crackers 7

Simply converting the JSON file into a dataframe yields: 在此处输入图像描述

Using json_normalize without setting any fields yields: 在此处输入图像描述

I tried setting the attributes (ie name, calories, ingredients, and serving_size) in the meta field and the dataframe looks similar to the dataframe when using json_normalize without any fields set.

Hopefully, I am overlooking something simple. Thanks in advance!

If dct is the dictionary from your question, then:

df = pd.DataFrame(
    [
        {"index": idx, **v}
        for idx, d in dct["liked_food"].items()
        for v in d.values()
    ],
).set_index("index")
print(df)

Prints:

             name  calories                    ingredients  serving_size
index                                                                   
A      cheesecake       500  sugar, eggs, cheese, crackers           7.0
A           donut       150             sugar, eggs, yeast           1.5
B      cheesecake       500  sugar, eggs, cheese, crackers           7.0

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