Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe to PowerBI's json format

I am trying to convert Dataframe data into PowerBI's JSON format. But no luck so far.

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

Expected JSON Format:

{
  "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"
    }
  ]
} 
like image 964
karthi-python Avatar asked Sep 11 '26 15:09

karthi-python


1 Answers

use pandas to_dict method :

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'}]}
like image 164
sammywemmy Avatar answered Sep 13 '26 05:09

sammywemmy