Python中如何用字典清洗含关键词列表的数据集列?
问题分析与解决
背景与需求
我有一个数据集,其中amenities列存储的是字符串形式的列表,样本数据如下:
'amenities': {1913: '[Cooking basics, Hair dryer, Fire extinguisher, Microwave, Refrigerator, Dedicated workspace, Pocket wifi, Lock on bedroom door, Dishes and silverware, Private living room, Essentials, Free street parking, Oven, Paid parking off premises, Hot water, Paid washer In building, Extra pillows and blankets, Hangers, Backyard, Shampoo, TV with standard cable, Stove, Heating, First aid kit, Self check-in, Iron, Smoke alarm, Lockbox, Bed linens, Kitchen, Patio or balcony, Coffee maker, Crib, Wifi]', 11765: '[Cooking basics, Hair dryer, Courtyard view, Coffee maker: drip coffee maker, Standalone high chair - available upon request, Outdoor furniture, Fire extinguisher, Coffee, Microwave, Shared backyard Fully fenced, Refrigerator, Dishes and silverware, TV, Essentials, Free washer In unit, Heating - split type ductless system, Oven, Hot water, Extra pillows and blankets, Toaster, Hangers, Long term stays allowed, Trash compactor, Shower gel, Private patio or balcony, Clothing storage: closet, Cleaning products, Drying rack for clothing, City skyline view, Shampoo, Central air conditioning, Garden view, Freezer, Hot water kettle, Host greets you, Dishwasher, Babysitter recommendations, Wifi, Elevator, Private entrance, First aid kit, Baby bath - available upon request, Safe, AC - split type ductless system, Room-darkening shades, Iron, Smoke alarm, Bed linens, Wine glasses, Kitchen, Body soap, Dining table, Sun loungers, Crib, Stainless steel electric stove, Conditioner, Pack n play/Travel crib - available upon request, Free parking on premises]', 9320: '[Air conditioning, Free street parking, Fire extinguisher, Dedicated workspace, First aid kit, Paid street parking off premises]'}
我需要用手动创建的替换字典(示例如下,完整含150+条目)清洗该列:检查列表中是否存在字典的键,并用对应值替换。
{'Silver refrigerator': 'Refrigerator', 'Electronia refrigerator': 'Refrigerator', 'Kunft refrigerator': 'Refrigerator', 'com zona de congelao refrigerator': 'Refrigerator', 'BEKO refrigerator': 'Refrigerator', 'Teka refrigerator': 'Refrigerator', 'Desconhecida refrigerator': 'Refrigerator', 'Pequeno com espao de congelao refrigerator': 'Refrigerator', 'SMEG refrigerator': 'Refrigerator', 'Indiferente refrigerator': 'Refrigerator', 'ORIMA refrigerator': 'Refrigerator', 'Hotpoint refrigerator': 'Refrigerator', 'JOCEL refrigerator': 'Refrigerator', 'Frigorico com congelador de encastre - BALAY refrigerator': 'Refrigerator', 'Grote Koelkast refrigerator': 'Refrigerator', 'SMEG refrigerator': 'Refrigerator', 'Samsung refrigerator': 'Refrigerator', 'Americano refrigerator': 'Refrigerator', 'Candy refrigerator': 'Refrigerator', 'Bosch refrigerator': 'Refrigerator', 'Lg refrigerator': 'Refrigerator', 'Resort access': 'Resort access'}
问题现象
我编写了如下函数,但执行后输出为由单个字母组成的嵌套列表,只有简单示例中该函数能正常运行:
def clean_words(word_list, replacement_dict): cleaned_words = [replacement_dict.get(word, word) for word in word_list] return cleaned_words df['amenities'] = df['amenities'].apply(clean_words, replacement_dict=replacement_dict)
问题根源
核心问题在于:你的amenities列存储的是字符串,不是真正的Python列表。当你对字符串调用apply(clean_words)时,函数里的word_list参数实际是这个字符串本身——而字符串是可迭代对象,会被逐个字符遍历,最终列表推导式会把每个字符单独处理,返回由单个字母组成的列表。
解决方案
需要先把字符串形式的列表转换成真正的Python列表,再执行替换逻辑,代码如下:
import ast def clean_amenities(amenity_str, replacement_dict): # 将字符串形式的列表解析为实际的Python列表 amenity_list = ast.literal_eval(amenity_str) # 遍历列表执行替换 cleaned_list = [replacement_dict.get(item, item) for item in amenity_list] # 可选:如果需要将结果转回字符串格式,取消下面一行的注释 # return str(cleaned_list) return cleaned_list df['amenities'] = df['amenities'].apply(clean_amenities, replacement_dict=replacement_dict)
注意事项
- 使用
ast.literal_eval而非eval:前者更安全,只会解析合法的Python字面量(比如列表、字典等),避免执行恶意代码的风险。 - 如果你的数据中存在格式不规范的字符串(比如引号不匹配、语法错误),可能需要先做预处理,否则
ast.literal_eval会抛出异常。
内容的提问来源于stack exchange,提问作者Ibrahim
相关产品推荐
相关产品推荐

