Home > Enterprise >  How to make a dataframe from a nested dict and make it receive its respective values?
How to make a dataframe from a nested dict and make it receive its respective values?

Time:10-15

I've been trying these last few days to create a dataframe from this object, I`ve got a dict object:

{'2022-10-11T14:00:00Z': {
         'INMET_CURITIBA_A807_M': {
                 'PRESS_HPA': [{'value': '915.0', 'quality': 'qf-GOOD', 'quality_flag': 'GOOD', 'structure': 'INMET_CURITIBA_A807_M', 'element': 'PRESS_HPA'}],

                 'PRESS_MX_HPA': [{'value': '82', 'quality': 'qf-UNCHECKED', 'quality_flag': 'UNCHECKED', 'structure': 'INMET_CURITIBA_A807_M', 'element': 'PRESS_MX_HPA'}]

              },

I want to do something like this:

                         PRESS_HPA       PRESS_MX_HPA   
2022-10-11T14:00:00Z        915               82               

I had more dates, but I cant show all object here...

CodePudding user response:

You can use this for your case, I hope this will work for you.

import pandas as pd
d = {'2022-10-11T14:00:00Z': {
         'INMET_CURITIBA_A807_M': {
                 'PRESS_HPA': [{'value': '915.0', 'quality': 'qf-GOOD', 'quality_flag': 'GOOD', 'structure': 'INMET_CURITIBA_A807_M', 'element': 'PRESS_HPA'}],

                 'PRESS_MX_HPA': [{'value': '82', 'quality': 'qf-UNCHECKED', 'quality_flag': 'UNCHECKED', 'structure': 'INMET_CURITIBA_A807_M', 'element': 'PRESS_MX_HPA'}]

              }},
    '2022-10-11T15:00:00Z': {
        'INMET_CURITIBA_A807_M': {
            'PRESS_HPA': [
                {'value': '915.0', 'quality': 'qf-GOOD', 'quality_flag': 'GOOD', 'structure': 'INMET_CURITIBA_A807_M',
                 'element': 'PRESS_HPA'}],

            'PRESS_MX_HPA': [{'value': '82', 'quality': 'qf-UNCHECKED', 'quality_flag': 'UNCHECKED',
                              'structure': 'INMET_CURITIBA_A807_M', 'element': 'PRESS_MX_HPA'}]

        }}
}

def convert_nested_dict(d):
    main_values, value = [], []
    inner = {}
    for dict1 in d.keys():
        for index in d[dict1].values():
            key = index.keys()
            for index2 in  index.values():
                value.append(index2[0]['value'])
            inner = dict(zip(key,value))
        main_values.append(inner)
    return main_values

df = pd.DataFrame(dict(zip(d.keys(),convert_nested_dict(d)))).T

The output what i got is,

                     PRESS_HPA PRESS_MX_HPA
2022-10-11T14:00:00Z     915.0           82
2022-10-11T15:00:00Z     915.0           82

Happy coding :).

  • Related