0

I've got a problem reading a json-file to pandas dataframe. I've got a largish json-file with structure like this:

`{"name":
         [{"header": -999.0,
           "header2": -999.0,
           "header3": -999.0,
            .....
           "headern": -999.0},
            .....
            .....

           {"header": -999.0,
           "header2": -999.0,
           "header3": -999.0}],
 "name2":
           [{"header": -999.0,
           "header2": -999.0,
           "header3": -999.0,
            .....
           "headern": -999.0},
            .....
            .....

           {"header": -999.0,
           "header2": -999.0,
           "header3": -999.0,]}

As expected data = pd.read_json(source,orient='index',lines=True) reads the file so that "names" are indexes and the headers and values to one single column per row. I would like the table to be in this form:

    header1 header2 ... header n
 0   -999   -999          -999
 1   -999   -999          -999
 2   -999   -999          -999

I tried to loop through the file with codes like this (and many others)

import pandas as pd


for index, row in data.iterrows():             
    df.append(pd.DataFrame.from_dict(row), ignore_index=True)

But I cannot get the correct structure (e.g. the example above gives empty df). I am new to python&pandas so I probably don't have proper understanding of dataframes, but after reading documentations multiple times I still couldn't figure this out.

Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
jtman
  • 1
  • 1
  • Follow this link - https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe It will solve your problem. – Kiran Prusty Dec 04 '18 at 17:48
  • Thanks! I found the solution (intially reading the file into an array) from the link above. – jtman Dec 04 '18 at 22:01

0 Answers0