首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从使用嵌套字典创建的数据帧中设置Pandas Hierarchical Multi-Index

从使用嵌套字典创建的数据帧中设置Pandas Hierarchical Multi-Index
EN

Stack Overflow用户
提问于 2019-05-10 21:40:46
回答 2查看 331关注 0票数 2

我有一个嵌套的字典,有3层,

代码语言:javascript
复制
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的答案

代码语言:javascript
复制
pd.concat({key:pd.DataFrame.from_dict(example_d[key],orient='columns')
              for key in example_d.keys()}).unstack(1)

这给了我:

但我需要多级列索引中的最低级别来尊重它们的父级。

也就是说,在colour标题下,我只希望显示彩色列,而在country标题下,我只希望看到国家列。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-10 21:55:08

首先更改dictionary,传递给Series构造函数并由Series.unstack进行整形

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2019-05-10 21:58:25

使用concat的IIUC

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56078901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档