【问题标题】:Set Pandas Hierarchical Multi-Index from a dataframe created using a nested dictionary从使用嵌套字典创建的数据框中设置 Pandas 分层多索引
【发布时间】:2019-09-28 10:33:23
【问题描述】:

我有一个 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 标题下,我只想看到国家/地区列。

【问题讨论】:

    标签: python pandas multi-index


    【解决方案1】:

    首先更改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
    

    【讨论】:

    • 两个答案都有效,但这一个对我来说有优势,因为它提出了一种清晰的方法来处理这个特定的例子,而且只需要一些简单的调整,也可以更普遍地应用于其他不同深度的嵌套字典 - 通过构建元组键索引 - 然后可以使用 stackunstack 进行管理。
    【解决方案2】:

    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
    

    【讨论】:

    • 这两个答案都以自己的方式简洁 - 这个最接近我开始的堆叠结构。
    猜你喜欢
    • 2021-11-24
    • 2021-02-04
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2018-06-03
    • 2017-03-08
    • 2022-07-01
    • 2021-12-08
    相关资源
    最近更新 更多