Python 从使用嵌套字典创建的数据帧设置分层多索引
我有一个三层嵌套字典Python 从使用嵌套字典创建的数据帧设置分层多索引,python,pandas,multi-index,Python,Pandas,Multi Index,我有一个三层嵌套字典 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},
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}}}
我想把它移动到一个数据框架中,这样行索引就来源于我字典的第一级,并使用剩余的级别作为分层列索引
我可以通过使用以下内容,修改以下内容的答案:
这给了我:
但我需要多级列索引中的最低级别来尊重他们的父母
i、 e.在颜色
标题下,我只希望显示颜色列,在国家
标题下,我只希望看到国家列 第一次更改,传递到系列
构造函数,并通过以下方式重塑:
IIUC使用
concat
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 3
这两个答案都有各自的特点——这一个最接近于我开始时的堆叠结构。这两个答案都有效,但这一个对我来说有优势,因为它不仅为处理这个特定的例子提供了一个清晰的方法,而且只提供了一些简单的调整,还可以更普遍地应用于其他不同深度的嵌套字典-通过构造元组键索引-然后可以使用
stack
和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
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 3