用正则表达式替换引号,逗号和撇号-python / pandas [英] replacing quotes, commas, apostrophes w/ regex - python/pandas

查看:119
本文介绍了用正则表达式替换引号,逗号和撇号-python / pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列带有地址的列,有时我想删除这些字符=> '- -(撇号,双引号,逗号)

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.

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.

您的帮助将不胜感激!

推荐答案

您可以使用 str.replace

You can use str.replace:

test['Address 1'] = test['Address 1'].str.replace(r"[\"\',]", '')

示例:

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

这篇关于用正则表达式替换引号,逗号和撇号-python / pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆