用正则表达式替换引号、逗号、撇号 - python/pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39214938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replacing quotes, commas, apostrophes w/ regex - python/pandas
提问by medev21
I have a column with addresses, and sometimes it has these characters I want to remove => '
- "
- ,
(apostrophe, double quotes, commas)
我有一列地址,有时它有我想删除的这些字符 => '
- "
- ,
(撇号、双引号、逗号)
I would like to replace these characters with space in one shot. I'm using pandas and this is the code I have so far to replace one of them.
我想一次性用空格替换这些字符。我正在使用Pandas,这是迄今为止我用来替换其中一个的代码。
test['Address 1'].map(lambda x: x.replace(',', ''))
Is there a way to modify these code so I can replace these characters in one shot? Sorry for being a noob, but I would like to learn more about pandas and regex.
有没有办法修改这些代码,以便我可以一次性替换这些字符?很抱歉我是个菜鸟,但我想了解更多关于 Pandas 和 regex 的信息。
Your help will be appreciated!
您的帮助将不胜感激!
回答by jezrael
You can use str.replace
:
您可以使用str.replace
:
test['Address 1'] = test['Address 1'].str.replace(r"[\"\',]", '')
Sample:
样本:
import pandas as pd
test = pd.DataFrame({'Address 1': ["'aaa",'sa,ss"']})
print (test)
Address 1
0 'aaa
1 sa,ss"
test['Address 1'] = test['Address 1'].str.replace(r"[\"\',]", '')
print (test)
Address 1
0 aaa
1 sass
回答by Brad123
Here's the pandas solution: To apply it to an entire dataframe use, df.replace. Don't forget the \ character for the apostrophe. Example:
这是Pandas解决方案:要将其应用于整个数据帧,请使用df.replace。不要忘记撇号的 \ 字符。例子:
import pandas as pd
df = #some dataframe
df.replace('\'','', regex=True, inplace=True)