Construct pandas DataFrame from items in nested dictionary

后端 未结 5 1901
名媛妹妹
名媛妹妹 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 11:01

    A pandas MultiIndex consists of a list of tuples. So the most natural approach would be to reshape your input dict so that its keys are tuples corresponding to the multi-index values you require. Then you can just construct your dataframe using pd.DataFrame.from_dict, using the option orient='index':

    user_dict = {12: {'Category 1': {'att_1': 1, 'att_2': 'whatever'},
                      'Category 2': {'att_1': 23, 'att_2': 'another'}},
                 15: {'Category 1': {'att_1': 10, 'att_2': 'foo'},
                      'Category 2': {'att_1': 30, 'att_2': 'bar'}}}
    
    pd.DataFrame.from_dict({(i,j): user_dict[i][j] 
                               for i in user_dict.keys() 
                               for j in user_dict[i].keys()},
                           orient='index')
    
    
                   att_1     att_2
    12 Category 1      1  whatever
       Category 2     23   another
    15 Category 1     10       foo
       Category 2     30       bar
    

    An alternative approach would be to build your dataframe up by concatenating the component dataframes:

    user_ids = []
    frames = []
    
    for user_id, d in user_dict.iteritems():
        user_ids.append(user_id)
        frames.append(pd.DataFrame.from_dict(d, orient='index'))
    
    pd.concat(frames, keys=user_ids)
    
                   att_1     att_2
    12 Category 1      1  whatever
       Category 2     23   another
    15 Category 1     10       foo
       Category 2     30       bar
    

提交回复
热议问题