pythonpandasdataframeseries

Pandas Unpack List of Dicts to Columns


I have a dataframe that has a field called fields which is a list of dicts (all rows have the same format). Here is how the dataframe is structured:

formId    fields
   123    [{'number': 1, 'label': 'Last Name', 'value': 'Doe'}, {'number': 2, 'label': 'First Name', 'value': 'John'}]

I am trying to unpack the fields column so it looks like:

formId    Last Name    First Name
   123          Doe          John

The code I have currently is:

for i,r in df.iterrows():
    for field in r['fields']:
        df.at[i, field['label']] = field['value']

However this does not seem like the most efficient way. Is there a better way to accomplish this?


Solution

  • Personally, I'd construct new dataframe:

    df = pd.DataFrame(
        [
            {"formId": form_id, **{f["label"]: f["value"] for f in fields}}
            for form_id, fields in zip(df["formId"], df["fields"])
        ]
    )
    
    print(df)
    

    Prints:

       formId Last Name First Name
    0     123       Doe       John