1

DO NOT MARK THIS AS DUPLICATE.

This is my dictionary, test_dict:

{"Report" : {
      "ReportHeader": {
      "ReportNum": None,
      "Type": {
        "source": "user",
        "text": "Training"
      },
      "Reg": "WWWWWW"
    }
}
}

I want to flatten as a dataframe where the expected output is:

    Report.ReportHeader.ReportNum     Report.ReportHeader.Type.source          Report.ReportHeader.Type.text     Report.ReportHeader.Reg 

                None                        User                                           Training                            WWWWWWW

What I've done so far :

data_df = pd.DataFrame.from_dict(test_dict)

Producing this in Dataframe:

                                              Report
ReportHeader  {'ReportNum': None, 'Type': {'source': ...

I also try to explode:

data_df = pd.DataFrame(test_dict).explode('Report').reset_index(drop=True)

But I'm not getting the desired output. Instead getting KeyError:0.

cna
  • 317
  • 2
  • 9
  • Does this answer your question? [Convert Python dict into a dataframe](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) – anddt Oct 07 '20 at 08:49
  • No @anddt clearly not, the duplicate dictionary is flat – yatu Oct 07 '20 at 08:53

1 Answers1

2
d = {"Report" : {
      "ReportHeader": {
      "ReportNum": None,
      "Type": {
        "source": "user",
        "text": "Training"
      },
      "Reg": "WWWWWW"
    }
}
}

df = pd.json_normalize(d)
print(df)

Prints:

  Report.ReportHeader.ReportNum Report.ReportHeader.Type.source Report.ReportHeader.Type.text Report.ReportHeader.Reg
0                          None                            user                      Training                  WWWWWW
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91