简体   繁体   English

如何使用以下 DataFrame 创建字典

[英]How to create a dictionary with the following DataFrame

Considering a dataframe with the following shape:考虑具有以下形状的 dataframe:

label      method      args
Cluster 0  relayout    ['shapes', 1]
Cluster 1  relayout    ['shapes', 3]
Cluster 2  relayout    ['shapes', 4]

I want to to create a dictionary that looks like this:我想创建一个看起来像这样的字典:

{'buttons': [
  {'label': 'Cluster 0', 'method': 'relayout', 'args': ['shapes', 1]},
  {'label': 'Cluster 1', 'method': 'relayout', 'args': ['shapes', 3]},
  {'label': 'Cluster 2', 'method': 'relayout', 'args': ['shapes', 4]},
  ]}

Using df.to_dict使用df.to_dict

res = {'buttons': df.to_dict(orient='records')}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM