简体   繁体   English

将 pandas dataframe 转换为嵌套字典

[英]Convert a pandas dataframe into a nested dictionary

I have a dataframe that I would like to convert into a nested dictionary.我有一个 dataframe,我想将其转换为嵌套字典。 For example:例如:

df = df =

ID ID Action行动 Responsible负责任的 Phase阶段
1.1 1.1 Request Document请求文件 Project Manager专案经理 1.0 Create Document Request 1.0 创建文档请求
2.1 2.1 Create course module创建课程模块 Writer作家 2.0 Create Document 2.0 创建文档
2.2 2.2 Send module for review发送模块以供审核 Writer作家 2.0 Create Document 2.0 创建文档
3.1 3.1 Publish Course发布课程 Reviewers审稿人 3.0 Publish Document 3.0 发布文件
3.2 3.2 Address feedback地址反馈 Writer作家 3.0 Publish Document 3.0 发布文件

Ultimately, I need to turn it into a nested dictionary that is something like this:最终,我需要将它变成一个像这样的嵌套字典:

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'},    
        ],
        }    
],
} 

I've thought of using df.groupby and to_dict and a lambda function, but I haven't figured out how to get it to work我想过使用df.groupbyto_dict以及lambda function,但我还没有弄清楚如何让它工作

(Sorry, I know this isn't the cleanest code or example; I'm still learning) (抱歉,我知道这不是最干净的代码或示例;我还在学习)

EDIT:编辑:

The code I have tried is:我试过的代码是:

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

but that provides the wrong output as it doesn't give the right keys for the dictionary.但这提供了错误的 output,因为它没有为字典提供正确的键。 And as I think about it, what I really need to do is create nested lists inside a dictionary, matched to the right key, grouped by the 'Phase'当我想到它时,我真正需要做的是在字典中创建嵌套列表,与正确的键匹配,按“阶段”分组

You can use to_dict within groupby, then use to_dict on the result again to get nested records:您可以在 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'}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM