简体   繁体   English

另一列的pandas str.replace值

[英]pandas str.replace value of a column from another column

I am kinda lost here, I am trying to replace a part of the str within one column that is matching value from a separate column. 我有点迷失在这里,我试图替换与另一列中的值匹配的一列中的str的一部分。

This is what i have so far 这就是我到目前为止

    brand       product           
0   BestBrand   prA by BestBrand    
1   newBrand    prB by newBrand 
2   GreatBrand  prC by GreatBrand

and I need something like this, 我需要这样的东西

    brand       product             newProductName
0   BestBrand   prA by BestBrand    prA
1   newBrand    prB by newBrand     prB
2   GreatBrand  prC by GreatBrand   prC

Tried these methods, 尝试了这些方法

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, '')) 

You can use apply and replace ie 您可以使用applyreplace

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM