gpt4 book ai didi

python - Dataframe 到 PowerBI 的 json 格式

转载 作者:行者123 更新时间:2023-12-04 10:14:09 25 4
gpt4 key购买 nike

我正在尝试将 Dataframe 数据转换为 PowerBI 的 JSON 格式。但到目前为止还没有运气。

数据框:

  ProductID Name                        Category    IsCompete ManufacturedOn
0 1 Adjustable Race Components true 07/30/2014
1 2 LL Crankarm Components false 07/30/2014
2 3 HL Mountain Frame - Silver Bikes true 07/30/2019

预期的 JSON 格式:

{
"rows": [
{
"ProductID": 1,
"Name": "Adjustable Race",
"Category": "Components",
"IsCompete": true,
"ManufacturedOn": "07/30/2014"
},
{
"ProductID": 2,
"Name": "LL Crankarm",
"Category": "Components",
"IsCompete": true,
"ManufacturedOn": "07/30/2014"
},
{
"ProductID": 3,
"Name": "HL Mountain Frame - Silver",
"Category": "Bikes",
"IsCompete": true,
"ManufacturedOn": "07/30/2014"
}
]
}

最佳答案

使用 Pandas to_dict方法:

json = {'rows':df.to_dict('records')}

print(json)

{'rows': [{'ProductID': 1,
'Name': 'Adjustable Race',
'Category': 'Components',
'IsCompete': True,
'ManufacturedOn': '07/30/2014'},
{'ProductID': 2,
'Name': 'LL Crankarm',
'Category': 'Components',
'IsCompete': False,
'ManufacturedOn': '07/30/2014'},
{'ProductID': 3,
'Name': 'HL Mountain Frame - Silver',
'Category': 'Bikes',
'IsCompete': True,
'ManufacturedOn': '07/30/2019'}]}

关于python - Dataframe 到 PowerBI 的 json 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61166318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com