我有一个包含一列词典列表的pandas数据框。
list_dicts
id
a1 [{name:'cat'}, {name:'dog'}]
a2 [{name:'toy'}, {name:'boy'}]
a3 [{name:'jack'},{name:'jill'},{name:'sam'}]
a4 [{name:'pig'}]
dicts列表中的每个键都是' name'。我想创建一个与' name'相关的所有值的列表。键并将新列附加到现有数据框,如下所示。
list_from_dict
id
a1 ['cat','dog']
a2 ['toy','boy']
a3 ['jack','jill','sam']
a4 ['pig']
我怎样才能做到这一点?我理解它可能会使用lambda函数,但不确定如何。
答案 0 :(得分:2)
你可以使用列表理解并且只在一行中没有lambda函数来执行此操作:
df['list_from_dict'] = [[x['name'] for x in list_dict] for list_dict in df['list_dicts']]
答案 1 :(得分:0)
一种方法是将pd.Series.apply
与自定义lambda
功能一起使用:
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]