【发布时间】: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.groupby 和to_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