str_replace_all()r等价于python

时间:2016-03-01 22:27:21

标签: python regex pandas dataframe

我正在从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

我想使用&替换部分'特征'匹配' name'每行的列。在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中实现这个目的,我会使用什么语法?

谢谢!

1 个答案:

答案 0 :(得分: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