我有一个字符串列表,我想在python数据框中的几列中替换这些字符串:
L= ["apple", "banana"]
df
columnA columnB
apple is green appleer is a name
bananas are good to eat banana is a fruit and no apples
具有理想的结果
df
columnA columnB
X is green appleer is a name
bananas are good to eat X is a fruit and no apples
现在我正在使用:
df['columnA'] =data['columnA'].replace(L, 'X', regex=True)
df['columnB'] =data['columnB'].replace(L, 'X', regex=True)
有更快的方法吗?它已经是矢量化解决方案。
最初的问题来自此帖子:Replacing few values in a column based on a list in python
还有一个类似的老问题,但是我不确定这是否仍是最新的:Fastest Python method for search and replace on a large string