我有点迷失在这里,我试图在一列中替换一个与一个单独的列匹配的值的str的一部分。
这是我到目前为止所拥有的
brand product
0 BestBrand prA by BestBrand
1 newBrand prB by newBrand
2 GreatBrand prC by GreatBrand
我需要这样的东西,
brand product newProductName
0 BestBrand prA by BestBrand prA
1 newBrand prB by newBrand prB
2 GreatBrand prC by GreatBrand prC
试过这些方法,
h = new_file.brand
new_file['newProductName']=new_file.product.str.replace(h, '')
new_file.loc[new_file['newProductName'], 'product']= new_file.product.apply(lambda x: x.replace(h, ''))
答案 0 :(得分:0)
您可以使用apply
和replace
即
df['new'] = df.apply(lambda x : x['product'].replace(('by '+x['brand']),''),1)
brand product new
0 BestBrand prA by BestBrand prA
1 BestBrand prB by BestBrand prB
2 BestBrand prC by BestBrand prC