从嵌套字典创建高格式数据框

时间:2019-03-13 22:03:42

标签: python pandas dataframe dictionary

嗨,我有一个字典词典,如图所示(示例)。我想将其转换为高DataFrame

示例字典:

{'Jill': {'Avenger: Age of Ultron': 7.0,
         'Django Unchained': 6.5,
         'Gone Girl': 9.0,
         'Kill the Messenger': 8.0},
 'Toby': {'Avenger: Age of Ultron': 8.5,
          'Django Unchained': 9.0,
          'Zoolander': 2.0}}

预期的高数据帧如下所示:

   Column1        Column2              Column3
    Jill     Avenger: Age of Ultron      7.0
    Jill     Django Unchained            6.5
    Jill     Gone Girl                   9.0
    Jill     Kill the Messenger          8.0
    Toby     Avenger: Age of Ultron      8.5
    Toby     Django Unchained            9.0
    Toby     Zoolander                   2.0

我尝试了以下操作,它把字典的主键作为第一列提供,但无法将其设置为高数据框:

pd.DataFrame.from_dict(d, orient='index')

5 个答案:

答案 0 :(得分:2)

d = {'Jill': {'Avenger: Age of Ultron': 7.0,
                            'Django Unchained': 6.5,
                            'Gone Girl': 9.0,
                            'Kill the Messenger': 8.0},
'Toby': {'Avenger: Age of Ultron': 8.5,
                                'Django Unchained': 9.0,
                                'Zoolander': 2.0}}

df = pd.DataFrame.from_dict(d).reset_index()
df = pd.melt(df, id_vars=["index"], 
                  var_name="By", value_name="Score").dropna()

答案 1 :(得分:0)

一种相当简单的方法是将字典转换为列表列表,并使用该列表来馈送数据框:

data = [[x, y, z] for x,v in d.items() for y, z in v.items()]
df = pd.DataFrame(data, columns=['Column' + str(i) for i in range(1,4)])

它给出:

  Column1                 Column2  Column3
0    Jill  Avenger: Age of Ultron      7.0
1    Jill        Django Unchained      6.5
2    Jill               Gone Girl      9.0
3    Jill      Kill the Messenger      8.0
4    Toby  Avenger: Age of Ultron      8.5
5    Toby        Django Unchained      9.0
6    Toby               Zoolander      2.0

答案 2 :(得分:0)

ratings = {'Jill': {'Avenger: Age of Ultron': 7.0,
                    'Django Unchained': 6.5,
                    'Gone Girl': 9.0,
                    'Kill the Messenger': 8.0},
           'Toby': {'Avenger: Age of Ultron': 8.5,
                    'Django Unchained': 9.0,
                    'Zoolander': 2.0}}
values = [[name, movie, rating] for name, r in ratings.items() for movie, rating in r.items()] 
df = pd.dataframe(values)

答案 3 :(得分:0)

只需传递给NULL,我们在DataFrame中处理的所有其他事情

pandas

答案 4 :(得分:0)

另一种方法,按原样在echo htmlspecialchars($example); 上使用pandas.DataFrame构造函数,然后依次使用stackingrenaming和对axiscolumns进行排序:

dict