[英]Convert (MultiIndex) Dataframe to a list of dicts
I have a multi index dataframe: 我有一个多索引数据框:
C
A B
25 58 16.0
59 135.0
60 36.0
which I want to convert to a list of dicts/objects: 我想转换为dicts / objects列表:
[
{A: 25, B: 58, C: 16},
{A: 25, B: 59, C: 135},
{A: 25, B: 60, C: 36}
]
I am able to reset the index df.reset_index
: 我能够重置索引df.reset_index
:
A B C
0 25 58 16.0
1 25 59 135.0
2 25 60 36.0
and use df.to_dict('index')
to get this: 并使用df.to_dict('index')
来获取:
[
{0: {A: 25, B: 58, C: 16}},
{1: {A: 25, B: 59, C: 135}},
{2: {A: 25, B: 60, C: 36}}
]
which is close, but I don't want to include the index in the resulting dict. 这是接近的,但我不想在最终的dict中包含索引。
Is there a simple way to achieve this? 有没有一种简单的方法来实现这一目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.