2016-03-01 86 views
2

我從R轉換到Python和具有樣品數據幀如下:str_replace_all()R等效在python

df = df = pd.DataFrame({'characterisitics': pd.Series(['Walter White made meth', 'Jessie Pinkman was called meth-head', 'Saul Goodman is always happy']), 'name': pd.Series(['Walter White', 'Jessie Pinkman', 'Saul Goodman'])}) 

     characteristics      name 
0    Walter White made meth  Walter White 
1 Jessie Pinkman was called meth-head  Jessie Pinkman 
2   Saul Goodman is always happy  Saul Goodman 

我想用替代的匹配「名稱」列,找出「特徵」的部分每一行。在R,我可以用:

str_replace_all(string = df$characteristics, pattern = fixed(df$name), replacement = '') 

而且我的輸出將如下所示:

 characteristics   name 
0    made meth Walter White 
1 was called meth-head Jessie Pinkman 
2  is always happy Saul Goodman 

我用什麼語法,如果我想在Python來實現這一目標?

謝謝!

回答

3

我認爲這一個你必須快速lambda應用到每一行。你實際上並不需要的正則表達式的簡單的例子,這樣的標準str.replace()正常工作:

df.apply(lambda row: row['characterisitics'].replace(row['name'], ''), axis='columns') 
Out[8]: 
0    made meth 
1  was called meth-head 
2   is always happy 
dtype: object 
+0

感謝那些工作! –