简体   繁体   English

Pandas DataFrame中嵌套的字典列表

[英]Nested List of Dictionaries in Pandas DataFrame

在此输入图像描述

Here the nutrients column had 4 dictionaries in a list, each dictionary has 5 keys with 1 values on each key. 营养素列在列表中有4个词典,每个词典有5个键,每个键上有1个值。

How can I flatten this nutrients column to make each as a sub column or row? 如何压扁这个营养素列,使每个作为子列或行?

Actually it was a JSON file and I already flattened it into this. 实际上它是一个JSON文件,我已经把它弄平了。 But I couldn't go further :( 但我无法走得更远:(

Thanks for the help. 谢谢您的帮助。

EDIT : Please see below for more information and what i tried: 编辑 :请参阅下面的更多信息和我尝试的:

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

You can use json_normalize() in order to flatten your JSON file like this: 你可以使用json_normalize()来平整你的JSON文件,如下所示:

import ujson
import pandas as pd

with open('/path/to/your/file.json') as f:
    data = ujson.load(f)

df = pd.io.json.json_normalize(data, 'nutrients', ['measure','name','ndbno','weight'])

assuming that ['measure','name','ndbno','weight'] - aren't nested 假设['measure','name','ndbno','weight'] - 不是嵌套的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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