我有一个嵌套的字典,有3层,
example_d = {'attribute_001': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_002': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_003': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_004': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_005': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}}}我想把它移到一个pandas数据帧中,这样行索引就在我的字典的第一级,并使用其余的级别作为分层的列索引。
我可以通过使用以下代码来接近,改编自here的答案
pd.concat({key:pd.DataFrame.from_dict(example_d[key],orient='columns')
for key in example_d.keys()}).unstack(1)这给了我:

但我需要多级列索引中的最低级别来尊重它们的父级。
也就是说,在colour标题下,我只希望显示彩色列,而在country标题下,我只希望看到国家列。
发布于 2019-05-10 21:55:08
首先更改dictionary,传递给Series构造函数并由Series.unstack进行整形
reform = {(level1_key, level2_key, level3_key): values
for level1_key, level2_dict in example_d.items()
for level2_key, level3_dict in level2_dict.items()
for level3_key, values in level3_dict.items()}
df = pd.Series(reform).unstack(level=[1,2])
print (df)
colour country
blue green red France Germany India UK USA
attribute_001 5 5 5 3 3 3 3 3
attribute_002 5 5 5 3 3 3 3 3
attribute_003 5 5 5 3 3 3 3 3
attribute_004 5 5 5 3 3 3 3 3
attribute_005 5 5 5 3 3 3 3 3发布于 2019-05-10 21:58:25
使用concat的IIUC
df= pd.DataFrame(example_d).T
pd.concat([df[x].apply(pd.Series) for x in list(df)],1,keys=list(df))
Out[540]:
colour country
blue green red France Germany India UK USA
attribute_001 5 5 5 3 3 3 3 3
attribute_002 5 5 5 3 3 3 3 3
attribute_003 5 5 5 3 3 3 3 3
attribute_004 5 5 5 3 3 3 3 3
attribute_005 5 5 5 3 3 3 3 3https://stackoverflow.com/questions/56078901
复制相似问题