def brand_describe(x):
if 'TRUM' in x.upper():
return "通快"
elif 'BYSTRONIC' in x.upper():
return "百超"
elif 'AMADA' in x.upper():
return "AMADA"
elif 'GWEIKE' in x.upper():
return "金威刻"
else:
return "其他"
def brand_describe(x):
brand_mapping = {'TRUM': '通快', 'BYSTRONIC': '百超', 'AMADA': 'AMADA', 'GWEIKE': '金威刻'}
# 遍历字典,查找匹配的品牌
for brand, description in brand_mapping.items():
if brand in x.upper():
return description
return "其他"
df['品牌'] = df['产品描述'].apply(brand_describe)
# 创建一个品牌映射字典
brand_mapping = {'TRUM': '通快', 'BYSTRONIC': '百超', 'AMADA': 'AMADA', 'GWEIKE': '金威刻'}
# 遍历字典并更新数据框
for keyword, brand in brand_mapping.items():
df.loc[df['产品描述'].str.contains(keyword), '品牌'] = brand
温馨提示:
上面代码中如果找不到包含的关键字,会显示为空,如果需要显示特定的字符如“其他”,需要在遍历前,将品牌这一列初始化为“其他”
df['品牌'] = '其他'