如何将包含元组列表的字典中的字典转换为熊猫数据框

时间:2021-03-03 06:04:10

标签: pandas list dataframe dictionary tuples

我有一本这样的字典

{
  'S1' :
     {
       'w1':[  ('a',0), ('b',1), ('c',3)],
       'w2':[  ('a',1), ('b',2), ('c',5)], 
       'w3':[  ('a',1), ('b',1), ('c',4)]
     },
 'S2' :      
    {
      'w4':[  ('a',1), ('b',2), ('c',5)],
      'w5':[  ('a',0), ('b',3), ('c',4)], 
      'w6':[  ('a',3), ('b',3), ('c',6)]
    }
}

我想把它转换成这样的熊猫数据框:

enter image description here

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

将嵌套列表理解与合并字​​典一起用于字典列表并传递给 DataFrame 构造函数:

L = [{**{'Col S': k,'Col W':k1}, **dict(v1)} for k, v in d.items() for k1, v1 in v.items()]

df = pd.DataFrame(L)
print (df)
  Col S Col W  a  b  c
0    S1    w1  0  1  3
1    S1    w2  1  2  5
2    S1    w3  1  1  4
3    S2    w4  1  2  5
4    S2    w5  0  3  4
5    S2    w6  3  3  6

MultiIndex 的解决方案:

d = {(k, k1): dict(v1) for k, v in d.items() for k1, v1 in v.items()}

df = pd.DataFrame.from_dict(d, orient='index')
print (df)
       a  b  c
S1 w1  0  1  3
   w2  1  2  5
   w3  1  1  4
S2 w4  1  2  5
   w5  0  3  4
   w6  3  3  6

然后设置索引名并将MultiIndex转成列,谢谢@sammywemmy:

df = df.rename_axis(index = ['Col S', 'Col W']).reset_index()
print (df)
  Col S Col W  a  b  c
0    S1    w1  0  1  3
1    S1    w2  1  2  5
2    S1    w3  1  1  4
3    S2    w4  1  2  5
4    S2    w5  0  3  4
5    S2    w6  3  3  6