DataBI为PowerBI的json格式

问题描述 投票:0回答:1

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

DataFrame:

  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"
    }
  ]
} 
python json pandas
1个回答
1
投票

使用熊猫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'}]}
© www.soinside.com 2019 - 2024. All rights reserved.