将字典列表的一列转换为列列表,以便从列表中每个字典下的键 "name" 派生值

Convert a column of list of dictionaries to a column list such that the values are derived from the key "name" under each dictionary in the list

输入列有可变数量的字典列表,它不是固定的。

INPUT column:

Facilities
[{'name': 'Work from home', 'icon': 'WFH.svg'}]
[{'name': 'Gymnasium', 'icon': 'Gym.svg'}, {'name': 'Cafeteria', 'icon': 'Cafeteria.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}]
[{'name': 'Free food', 'icon': 'FreeFood.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Education assistance', 'icon': 'Education.svg'}]
[{'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}, {'name': 'Job training', 'icon': 'JobTraining.svg'}]
[{'name': 'Free transport', 'icon': 'Transportation.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}]

上面的输入应该被过滤,这样该列将只有一个列表,其中包含从列表中不同词典收集的键“名称”的所有值。

Desired Output column:

Facilities
['Work from home']
['Gymnasium', 'Cafeteria', 'Work from home']
['Free food','Team outings','Education assistance']
['Soft skill training','Job training']
['Free transport', 'Work from home','Team outings','Soft skill training']

假设你有这个 DataFrame:

df = pd.DataFrame({'Facilities':[
[{'name': 'Work from home', 'icon': 'WFH.svg'}],
[{'name': 'Gymnasium', 'icon': 'Gym.svg'}, {'name': 'Cafeteria', 'icon': 'Cafeteria.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}],
[{'name': 'Free food', 'icon': 'FreeFood.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Education assistance', 'icon': 'Education.svg'}],
[{'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}, {'name': 'Job training', 'icon': 'JobTraining.svg'}],
[{'name': 'Free transport', 'icon': 'Transportation.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}],
    ]})

print(df)

                                          Facilities
0    [{'name': 'Work from home', 'icon': 'WFH.svg'}]
1  [{'name': 'Gymnasium', 'icon': 'Gym.svg'}, {'n...
2  [{'name': 'Free food', 'icon': 'FreeFood.svg'}...
3  [{'name': 'Soft skill training', 'icon': 'Soft...
4  [{'name': 'Free transport', 'icon': 'Transport...

然后:

df['Facilities'] = df['Facilities'].apply(lambda x: [d['name'] for d in x])
print(df)

打印:

                                          Facilities
0                                   [Work from home]
1             [Gymnasium, Cafeteria, Work from home]
2    [Free food, Team outings, Education assistance]
3                [Soft skill training, Job training]
4  [Free transport, Work from home, Team outings,...

你可以用两个列表理解来提取它:

facility_names = [[facility["name"] for facility in facility_list] for facility_list in facilities]

假设您的输入数据是:

facilities=[
[{'name': 'Work from home', 'icon': 'WFH.svg'}],
[{'name': 'Gymnasium', 'icon': 'Gym.svg'}, {'name': 'Cafeteria', 'icon': 'Cafeteria.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}],
[{'name': 'Free food', 'icon': 'FreeFood.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Education assistance', 'icon': 'Education.svg'}],
[{'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}, {'name': 'Job training', 'icon': 'JobTraining.svg'}],
[{'name': 'Free transport', 'icon': 'Transportation.svg'}, {'name': 'Work from home', 'icon': 'WFH.svg'}, {'name': 'Team outings', 'icon': 'TeamOuting.svg'}, {'name': 'Soft skill training', 'icon': 'SoftSkillsTraining.svg'}]
]