[Python] Convert dictionary rows to new dataframe

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    After importing some nested JSON data, I'm trying to create a new dataframe from all of the dictionary key / value pairs in an existing column.

    Starting point:

    >>> df['schedules']
    0 {'3263524': 'Group 1 CORE DAYS', '3263525': 'Group 1 CORE NIGHTS', '3263526': 'Group 1 EDUCATION', '3263527': 'Group 1 ROUNDING'}
    1 {'3263524': 'Group 1 CORE DAYS', '3881368': 'VS Days', '3881370': 'VS Education Shift A', '3881455': 'VS Education Shift B'}
    ...


    Desired output:

    >>> df_schedules
    id schedule_name
    0 3263524 Group 1 CORE DAYS
    1 3263525 Group 1 CORE NIGHTS
    2 3263526 Group 1 EDUCATION
    3 3263527 Group 1 ROUNDING
    4 3881368 VS Days
    5 3881370 VS Education Shift A
    6 3881455 VS Education Shift B


    I can get close with pd.DataFrame.from_dict, but need to name the columns and create an index.

    df_schedules = pd.DataFrame.from_dict(df['schedules'].iloc[0], orient='index')

    Continue reading...

Compartilhe esta Página