【问题标题】:3 level dictionary to dataframe in PythonPython中的3级字典到数据框
【发布时间】:2017-01-19 11:13:16
【问题描述】:

我有一个 3 级字典,我需要将其转换为 Python 中的数据框。 我对 python 相当陌生,并尝试在循环中运行循环以将文件写入数据帧,但没有给我所需的输出。

当前字典格式: enter image description here

所需的数据框: enter image description here

任何帮助将不胜感激!

字典:

{
    "60354": [
        {
            "http://www.aaaaaaaaaaaaaaaaaaaa.com": [
                {
                    "Garden fresh": "Best frozen peas", 
                    "Soooo flavorful!": "I typically" 
                }
            ], 
            "http://www.bbbbbbbbbbbbbbbbbbbbbbbb.com": [
                {
                    "good food": "a very good product", 
                    "Taste is fresh": "Very good frozen peas"
                }
            ]
        }
    ], 
    "66019": [
        {
            "http://www.cccccccccccccccccccccccccccccc.com": [
                {
                    "YUM!": "Tastes really good", 
                    "Very good": "Glad to have tried them"
                }
            ], 
            "http://www.ddddddddddddddddddddddddddddddddd.com": [
                {
                    "Amazing!": "I used orange juice", 
                    "Awesome": "Definitely will try other flavors" 
                }
            ]
        }
    ]
}

【问题讨论】:

  • 您的输入格式错误,从不关闭方括号
  • 你能提供你的字典代码而不是图片吗?
  • @Boud,图像是文件的 sn-p,因此格式不正确。我现在上传了正确的文件。
  • @Jarad,我已经添加了字典

标签: python json pandas dictionary


【解决方案1】:
def order_from_chaos(data):
    result = []
    ids = [id_ for id_ in data]
    id_content = [data[id_][0] for id_ in data]
    links_in_id_content = [dictionary.keys() for dictionary in id_content]

    for i in range(len(ids)):
        id_ = ids[i]
        links = links_in_id_content[i]
        descriptions = [id_content[i][key][0] for key in links_in_id_content[i]]
        content = zip(links, descriptions)
        for elem in list(content):
            link = elem[0]
            description_list = elem[1].items()
            for description in description_list:
                formatted_data = id_, link, description[0], description[1]
                result.append(formatted_data)
    return result

data = {
    "60354": [
        {
            "http://www.aaaaaaaaaaaaaaaaaaaa.com": [
                {
                    "Garden fresh": "Best frozen peas",
                    "Soooo flavorful!": "I typically"
                }
            ],
            "http://www.bbbbbbbbbbbbbbbbbbbbbbbb.com": [
                {
                    "good food": "a very good product",
                    "Taste is fresh": "Very good frozen peas"
                }
            ]
        }
    ],
    "66019": [
        {
            "http://www.cccccccccccccccccccccccccccccc.com": [
                {
                    "YUM!": "Tastes really good",
                    "Very good": "Glad to have tried them"
                }
            ],
            "http://www.ddddddddddddddddddddddddddddddddd.com": [
                {
                    "Amazing!": "I used orange juice",
                    "Awesome": "Definitely will try other flavors"
                }
            ]
        }
    ]
}


result = order_from_chaos(data)
[print(res) for res in result]

会给你

('60354', 'http://www.bbbbbbbbbbbbbbbbbbbbbbbb.com', 'Taste is fresh', 'Very good frozen peas')
('60354', 'http://www.bbbbbbbbbbbbbbbbbbbbbbbb.com', 'good food', 'a very good product')
('60354', 'http://www.aaaaaaaaaaaaaaaaaaaa.com', 'Soooo flavorful!', 'I typically')
('60354', 'http://www.aaaaaaaaaaaaaaaaaaaa.com', 'Garden fresh', 'Best frozen peas')
('66019', 'http://www.cccccccccccccccccccccccccccccc.com', 'Very good', 'Glad to have tried them')
('66019', 'http://www.cccccccccccccccccccccccccccccc.com', 'YUM!', 'Tastes really good')
('66019', 'http://www.ddddddddddddddddddddddddddddddddd.com', 'Awesome', 'Definitely will try other flavors')
('66019', 'http://www.ddddddddddddddddddddddddddddddddd.com', 'Amazing!', 'I used orange juice')

Process finished with exit code 0

根本不是很酷的结构。 但无论如何进行它的方式很混乱

【讨论】:

  • 最好的方法是 {'id': {'link': 'http://...', 'description': ['desc1', 'desc2', '...'] }}
  • 这对我有用。我需要它以数据框的形式出现,这在这之后简直是小菜一碟。谢谢
猜你喜欢
  • 2017-10-15
  • 2013-09-12
  • 2016-04-07
  • 2021-11-10
  • 2017-03-01
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多