0

This is the datframe.

dataFrame =pd.DataFrame(['yes',10,'NaN',200703727.0,2141219]).T
dataFrame.columns=["a",'B','C','D','E']

DataFrame-->>

a   B   C   D   E
0   yes 10  NaN 2.00704e+08 2141219

and I want to convert this dataframe into nested JSON n rows the numbers of rows could be million in datframe.

json output I need

{   "E":2141219,
    "AA":[  {name:'a',type:'yes'},
            {name:'B',type:'10'},
            {name:'C',type:'NaN'},
            {name:'D',type:'200703727.0'}
        ]
}

I have to add name and type. Please help me to solve it.

  • That's just for one row, what should be the output for a dataframe with more than one row? Also, what you are asking for is a custom `JSON` which is not directly possible with what pandas offer, so you need to take a look at `json` module in python, and convert each row manually. You can use `dataframe.apply` then convert each row to the required json format. – ThePyGuy Aug 03 '21 at 19:17

2 Answers2

0

You're gonna need to create a dictionary, or a list and then convert it to json

import json
dictionary = df1.to_dict(what you want to convert here)
json_1 = json.dumps
0

Have you tried to_json?

Also checkout:

af3ld
  • 782
  • 8
  • 30