【问题标题】:Convert a pandas dataframe into a nested dictionary将 pandas 数据框转换为嵌套字典
【发布时间】:2021-11-28 02:32:18
【问题描述】:

我有一个要转换为嵌套字典的数据框。例如:

df =

ID Action Responsible Phase
1.1 Request Document Project Manager 1.0 Create Document Request
2.1 Create course module Writer 2.0 Create Document
2.2 Send module for review Writer 2.0 Create Document
3.1 Publish Course Reviewers 3.0 Publish Document
3.2 Address feedback Writer 3.0 Publish Document

最后,我需要把它变成一个像这样的嵌套字典:

context = {'Section': 

[{'Phase': '1.0 Create Document',
   'Activity': [
            {'Responsible': 'Project Manager', 'ID': '1.1', 'Action': 'Request Document'},
            ],
        }, 
 {'Phase': '2.0 Create Document',
  'Activity': [
            {'Responsible': 'Writer', 'ID': '2.1', 'Action': 'Create course module'},
            {'Responsible': 'Writer', 'ID': '2.2', 'Action': 'Send module for review'},    
        ],
        },
{'Phase': '3.0 Publish Document',
  'Activity': [
            {'Responsible': 'Reviewers', 'ID': '3.1', 'Action': 'Publish course'},
            {'Responsible': 'Writer', 'ID': '3.2', 'Action': 'Address Feedback'},    
        ],
        }    
],
} 

我曾想过使用df.groupbyto_dict 以及lambda 函数,但我还没有弄清楚如何让它工作

(抱歉,我知道这不是最简洁的代码或示例;我还在学习)

编辑:

我试过的代码是:

context = df.groupby('Phase')[['ID','Action','Responsible','Note','Output']].apply(lambda x: x.set_index('ID').to_dict(orient='index')).to_dict()

但这提供了错误的输出,因为它没有为字典提供正确的键。在我思考的时候,我真正需要做的是在字典中创建嵌套列表,匹配正确的键,按“阶段”分组

【问题讨论】:

  • 根据预期的输出,我认为更好的方法是使用自定义函数将熊猫数据帧转换为预期的输出。
  • @TonyMontana 完全同意。我就是想不通
  • 好吧,您可以从一个函数开始,您将在其中循环遍历数据框并根据您的要求创建字典。

标签: python pandas dataframe dictionary


【解决方案1】:

您可以在 groupby 中使用to_dict,然后在结果上再次使用to_dict 以获取嵌套记录:

data = (df.drop('Phase', axis=1) 
          .groupby(df['Phase'])
          .apply(lambda x: x.to_dict(orient='r'))
          .reset_index(name='Activity')
          .to_dict(orient='r'))

context = {'Section': data}
print(context)
{'Section': [{'Activity': [{'Action': 'Request Document',
                            'ID': 1.1,
                            'Responsible': 'Project Manager'}],
              'Phase': '1.0 Create Document Request'},
             {'Activity': [{'Action': 'Create course module',
                            'ID': 2.1,
                            'Responsible': 'Writer'},
                           {'Action': 'Send module for review',
                            'ID': 2.2,
                            'Responsible': 'Writer'}],
              'Phase': '2.0 Create Document'},
             {'Activity': [{'Action': 'Publish Course',
                            'ID': 3.1,
                            'Responsible': 'Reviewers'},
                           {'Action': 'Address feedback',
                            'ID': 3.2,
                            'Responsible': 'Writer'}],
              'Phase': '3.0 Publish Document'}]}

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2015-10-06
    • 2021-12-21
    • 2021-06-30
    • 2018-06-03
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多