【发布时间】:2015-04-04 19:09:05
【问题描述】:
如何将嵌套字典转换为二维表
data[0]是表格行的集合
data[0][0]是表格行,
key:year是列名,
key:values 是列中的值。
我想将 data[0] 恢复为 Pandas 数据框中的表格形式。
我发现 json_normalize 可能会有所帮助,但不知道该怎么做。
有什么建议吗?
预期格式
字典中的原始数据
(Pdb++) data[0]
[{u'values': [{u'Actual': u'(0.2)'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2009'}, {u'values': [{u'Actual': u'2.8'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2010'}, {u'values': [{u'Actual': u'2.0'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2011'}, {u'values': [{u'Actual': u'2.0'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2012'}, {u'values': [{u'Actual': u'2.5'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2013'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.0'}, {u'Upper End of Central Tendency': u'3.0'}, {u'Lower End of Central Tendency': u'2.8'}, {u'Lower End of Range': u'2.1'}], u'year': u'2014'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.5'}, {u'Upper End of Central Tendency': u'3.2'}, {u'Lower End of Central Tendency': u'3.0'}, {u'Lower End of Range': u'2.2'}], u'year': u'2015'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'3.4'}, {u'Upper End of Central Tendency': u'3.0'}, {u'Lower End of Central Tendency': u'2.5'}, {u'Lower End of Range': u'2.2'}], u'year': u'2016'}, {u'values': [{u'Actual': u'-'}, {u'Upper End of Range': u'2.4'}, {u'Upper End of Central Tendency': u'2.3'}, {u'Lower End of Central Tendency': u'2.2'}, {u'Lower End of Range': u'1.8'}], u'year': u'Longer Run'}]
(Pdb++) data[0][0]
{u'values': [{u'Actual': u'(0.2)'}, {u'Upper End of Range': u'-'}, {u'Upper End of Central Tendency': u'-'}, {u'Lower End of Central Tendency': u'-'}, {u'Lower End of Range': u'-'}], u'year': u'2009'}
也许改变 JSON 模式会是一个更好的解决方案?
如果是这样,哪种新的 JSON 模式设计更适合这种表数据。谢谢
【问题讨论】: