-1

I have a dataframe as under

ID  Ph No
0   (090)993322
1   +93 11223344
2   91-935568
3   996688552
4   91 558866225
5   996633...

I want to remove all the special characters and spaces from the column Ph No. I used the below code

# Cleaning the phone numbers for special characters and spaces
erase = [",","-","_",".","+","(",")", " "]

for i in erase:
    #print(i)
    df['New Ph No'] = df['Ph No'].astype(str).str.replace(i,"",regex=True)

Though I didn't get any errors, when I study the output there are certain rows still containing special character like + or ( or ... and white spaces in-between numbers. I even tried .replace(" ","") on the output but that also does not seem to work.

How do i go about this?

Sid
  • 167
  • 7
  • 1
    Just use `df['New Ph No'] = df['Ph No'].astype(str).str.replace(r'\D+','', regex=True)` to remove all non-digit chars. If you want to only remove special chars, replace `\D` with `[\W_]+`. – Wiktor Stribiżew Jun 02 '22 at 11:47
  • 1
    The immediate problem with your solution is that you do not escape special regex metacharacters. Simply do not use a regex here if you need to replaced fixed chars, remove `,regex=True`. – Wiktor Stribiżew Jun 02 '22 at 11:49

0 Answers0