guru
guru

Reputation: 173

Unpack list of dicts into list in pandas dataframe

I have a pandas dataframe that includes a column of lists of dictionaries.

    list_dicts
id  
a1  [{name:'cat'}, {name:'dog'}]
a2  [{name:'toy'}, {name:'boy'}] 
a3  [{name:'jack'},{name:'jill'},{name:'sam'}] 
a4  [{name:'pig'}] 

Every key in the list of dicts is 'name'. I want to create a list of all the values associated with the 'name' keys and append the new column to the existing dataframe, as shown below.

    list_from_dict
id  
a1  ['cat','dog']
a2  ['toy','boy'] 
a3  ['jack','jill','sam'] 
a4  ['pig'] 

How can I achieve this? I understand it'll probably use a lambda function, but not sure how.

Upvotes: 1

Views: 2191

Answers (2)

Justin Malin
Justin Malin

Reputation: 98

You could do this with list comprehension and without a lambda function in just one line:

df['list_from_dict'] = [[x['name'] for x in list_dict] for list_dict in df['list_dicts']]

Upvotes: 3

jpp
jpp

Reputation: 164773

One way is to use pd.Series.apply with a custom lambda function:

df = pd.DataFrame({'list_dicts': [[{'name': 'cat'}, {'name': 'dog'}],
                                  [{'name': 'toy'}, {'name': 'boy'}],
                                  [{'name': 'jack'}, {'name': 'jill'}, {'name': 'sam'}],
                                  [{'name': 'pig'}]]},
                  index=['a1', 'a2', 'a3', 'a4'])

df['list_dicts'] = df['list_dicts'].apply(lambda x: [next(iter(d.values())) for d in x])

print(df)

           list_dicts
a1         [cat, dog]
a2         [toy, boy]
a3  [jack, jill, sam]
a4              [pig]

Upvotes: 0

Related Questions