Search code examples
pythonpandasdataframejson-normalize

Flattened and Convert list of Nested Dictionary to Pandas Dataframe


I have list of nested dictionary,with slightly different structure. I need to convert it into dataframe.

Nested dictionary-

dct = [{'2022-03-31': {'A': 12323, 'B': 123123},{'2021-03-31': {'A': 12, 'B': 123}}]

I tried pd.json_normalize(dict) but ,it didn't work properly because of date.

df = pd.json_normalize(dct)
df
Output-
    2022-03-31.A     2022-03-31.B    2021-03-31.A    2021-03-31.B
0       12323            123123          NAN             NAN
1        NAN               NAN           12               123

Expected Output-

    Date          A            B
0   2022-03-31   12323        123123
1   2021-03-31    12          123

Solution

  • Lets flatten the nested dict into list of records, then create a new dataframe

    pd.DataFrame({'date': k, **v} for d in dct for k, v in d.items())
    

             date      A       B
    0  2022-03-31  12323  123123
    1  2021-03-31     12     123