Search code examples
pythonpandasdataframedictionaryexplode

Flatten dictionary to dataframe


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.


Solution

  • 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