Quantcast
Channel: User Bijan - Stack Overflow
Viewing all articles
Browse latest Browse all 99

Pandas Unpack List of Dicts to Columns

$
0
0

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?


Viewing all articles
Browse latest Browse all 99

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>