简体   繁体   English

将带有字典列表的嵌套字典转换为 Pandas DataFrame

[英]Convert a nested dictionary with lists of dictionaries to a Pandas DataFrame

I have a dictionary like this:我有一本这样的字典:

{
    'person1' : {
        'category1' : [
            {'time' : time1, 'property1' : value1, 'property2' : value2, ...},
            {'time' : time2, 'property1' : value3, 'property2' : value4, ...},
            ...
        ],
        'category2' : [
            {...}, 
            ...
        ],
        ...
    },
    'person2' : {
        'category1' : [
            {'time' : time3, 'property1' : value5, 'property2' : value6, ...},
            {'time' : time4, 'property1' : value7, 'property2' : value8, ...},
        ],
        'category2' : [
            {...},
            ...
        ],
        ...
    }
}

that I'd like to convert to a DataFrame like this:我想像这样转换为 DataFrame:

'time'    'person'    'category'    'property1'    'property2'  ...  'propertyn'
--------------------------------------------------------------------------------
 time1     person1     category1      value1         value2     ...     value
 time2     person1     category1      value3         value4     ...     value
  ...      person1     category2       ...            ...       ...      ...
                                        .
                                        .
                                        .
 time3     person2     category1      value5         value6     ...     value
 time4     person2     category1      value7         value8     ...     value
  ...      person2     category2       ...            ...       ...      ...
                                        .
                                        .
                                        .
 timen     personn     category1      valuen         valuen     ...     value

I thought about using traversing through the dictionary with for-loops, but I'd like to avoid them to make this conversion as efficient as possible.我考虑过使用 for 循环遍历字典,但我想避免它们以使这种转换尽可能高效。 I've also looked into pd.DataFrame.from_dict() , but it's not enough.我还研究了pd.DataFrame.from_dict() ,但这还不够。

This answer is the closest I've seen, but their inner lists are just lists of values instead of a list of dictionaries like mine.这个答案是我见过的最接近的,但它们的内部列表只是值列表,而不是像我这样的字典列表。

I would appreciate any help with this conversion!对于此转换,我将不胜感激!

You can create a small dataframe for each person/category and concat them:您可以为每个person/category创建一个小的 dataframe 并将它们连接起来:

pd.concat([pd.DataFrame(u).assign(category=c,person=p) for p,data in d.items() for c,u in data.items()])

Output: Output:

    time property1 property2   category   person
0  time1    value1    value2  category1  person1
1  time2    value3    value4  category1  person1
0  time3    value5    value6  category1  person2
1  time4    value7    value8  category1  person2
0  time3    value5    value6  category2  person2
1  time4    value7    value8  category2  person2

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

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