Construct pandas DataFrame from items in nested dictionary

后端 未结 5 1897
名媛妹妹
名媛妹妹 2020-11-22 10:49

Suppose I have a nested dictionary \'user_dict\' with structure:

  • Level 1: UserId (Long Integer)
  • Level 2: Category (S
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 10:59

    In case someone wants to get the data frame in a "long format" (leaf values have the same type) without multiindex, you can do this:

    pd.DataFrame.from_records(
        [
            (level1, level2, level3, leaf)
            for level1, level2_dict in user_dict.items()
            for level2, level3_dict in level2_dict.items()
            for level3, leaf in level3_dict.items()
        ],
        columns=['UserId', 'Category', 'Attribute', 'value']
    )
    
        UserId    Category Attribute     value
    0       12  Category 1     att_1         1
    1       12  Category 1     att_2  whatever
    2       12  Category 2     att_1        23
    3       12  Category 2     att_2   another
    4       15  Category 1     att_1        10
    5       15  Category 1     att_2       foo
    6       15  Category 2     att_1        30
    7       15  Category 2     att_2       bar
    

    (I know the original question probably wants (I.) to have Levels 1 and 2 as multiindex and Level 3 as columns and (II.) asks about other ways than iteration over values in the dict. But I hope this answer is still relevant and useful (I.): to people like me who have tried to find a way to get the nested dict into this shape and google only returns this question and (II.): because other answers involve some iteration as well and I find this approach flexible and easy to read; not sure about performance, though.)

提交回复
热议问题