我想在输出到csv之前重新格式化我的嵌套字典。 我的嵌套词典:
review = {'Q1': {'Question': 'question wording','Answer': {'Part 1': 'Answer part one', 'Part 2': 'Answer part 2'} ,'Proof': {'Part 1': 'The proof part one', 'Part 2': 'The proof part 2'}},
'Q2': {'Question': 'question wording','Answer': {'Part 1': 'Answer part one', 'Part 2': 'Answer part 2'} ,'Proof': {'Part 1': 'The proof part one', 'Part 2': 'The proof part 2'}}}
到目前为止,我已经尝试过:
^{pr2}$然后分道扬镳:
Q1 Answer {'Part 1': 'Answer part one', 'Part 2': 'Answe...
Proof {'Part 1': 'The proof part one', 'Part 2': 'Th...
Question question wording
Q2 Answer {'Part 1': 'Answer part one', 'Part 2': 'Answe...
Proof {'Part 1': 'The proof part one', 'Part 2': 'Th...
Question question wording
但我希望最后看起来像这样:
Index Question Answer Proof
Q1 question one wording Answer part 1 Proof part 1
Q1 question one wording Answer part 2 Proof part 2
Q2 question two wording Answer part 1 Proof part 1
Q2 question two wording Answer part 2 Proof part 2
所以我需要熔化/取消堆叠/透视/展开/其他操作_word数据帧中的嵌套字典。在
我看了这篇文章以寻求指导,但无法将其应用于我自己: Expand pandas dataframe column of dict into dataframe columns
以下是一个潜在的解决方案:
1)创建具有方向“index”的初始
DataFrame
2)使用^{} 、^{} 和{a3}创建最终
^{pr2}$DataFrame
的形状3)通过传递给
DataFrame
构造函数并使用stack
值来修复“Answer”和“Proof”列相关问题 更多 >
编程相关推荐