[英]How to make a pandas dataframe with nested columns from a list of nested dicts
I have a list data
containing dicts with this schema:我有一个包含具有此架构的字典的列表
data
:
{'id': '123',
'2020-06-15': {'A': 1, 'B': 2, 'C': 3, 'D': 4},
'2020-06-16': {'A': 5, 'B': 6, 'C': 7, 'D': 8}}
When I use df = pandas.DataFrame(data).set_index("id")
to create a dataframe I get this table:当我使用
df = pandas.DataFrame(data).set_index("id")
创建 dataframe 时,我得到了这张表:
2020-06-15 2020-06-16
id
123 {'A': 1, 'B': 2, 'C': 3, 'D': 4} {'A': 5, 'B': 6, 'C': 7, 'D': 8}
And when I use df = pandas.json_normalize(data)
I get this:当我使用
df = pandas.json_normalize(data)
我得到这个:
id 2020-06-15.A 2020-06-15.B 2020-06-15.C 2020-06-15.D 2020-06-16.A 2020-06-16.B 2020-06-16.C 2020-06-16.D
0 123 1 2 3 4 5 6 7 8
But preferred result is this:但首选的结果是:
2020-06-15 2020-06-16
id A B C D A B C D
123 1 2 3 4 5 6 7 8
Is there any way to make it happen?有没有办法让它发生?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.