replacing quotes, commas, apostrophes w/ regex - python/pandas

15,216

Solution 1

You can use 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

Solution 2

Here's the pandas solution: To apply it to an entire dataframe use, df.replace. Don't forget the \ character for the apostrophe. Example:

import pandas as pd
df = #some dataframe
df.replace('\'','', regex=True, inplace=True) 
Share:
15,216
medev21
Author by

medev21

Just a dude who enjoys coding, day and night. A ronin coder, didn't go to school or "dojo" for programming so I don't have a "master".

Updated on June 16, 2022

Comments

  • medev21
    medev21 almost 2 years

    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.

    Your help will be appreciated!