[Python] Convert nested python dict to a pandas dataframe

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have weather data in a python dict that I'm trying to convert to a pandas df. (From there I'll load it into SQLServer but I've got that part working)

    my_dict = {
    'data.outdoor.temperature': {'unit': '℃', 'list':
    {'datetime.datetime(2025, 4, 23, 10, 0): 22.3',
    'datetime.datetime(2025, 4, 23, 14, 0): 21.3',
    'datetime.datetime(2025, 4, 23, 18, 0): 18.2', }},
    'data.indoor.temperature': {'unit': '℃', 'list':
    {'datetime.datetime(2025, 4, 23, 10, 0): 23.2',
    'datetime.datetime(2025, 4, 23, 14, 0): 23.5',
    'datetime.datetime(2025, 4, 23, 18, 0): 22.9'}}}


    The output I'm after is:

    outdoor.temperature | 2025_04_20 | 14 | 28.1 | ℃
    outdoor.temperature | 2025_04_20 | 18 | 23.8 | ℃
    .....etc
    indoor.temperature | 2025_04_20 | 14 | 23.5 | ℃
    indoor.temperature | 2025_04_20 | 18 | 23.8 | ℃
    .....etc


    The list of datetime & temperatures could be 100s of records

    I've tried from_dict, concat / unstack, normalize, flatten and combinations of the lot but I'm beaten

    Continue reading...

Compartilhe esta Página