On Python 3.9 and Pandas 1.3.4.
So I'm trying to get rid of "(". ")", and "-" from my csv file in column E.
This is what the original file looks like. This is what I'm trying to get.
Here is my code (it is editted to work):
import pandas as pd
df = pd.read_csv('file.csv', header=0)
df['Phone'] = df['Phone'].str.replace(['()-'], ' ')
df.to_csv('file.csv')
But this is what happens. I was looking at this site and another question on here that I have lost on how to do a find and replace but it does not work/
CodePudding user response:
You have to convert you pandas Series to string (df['Phone'].str
) before calling the replace
method.
df['Phone'].str.replace('(', ' ', regex=True)
should do the trick.
CodePudding user response:
Try using regex character class [()-]
to remove any unwanted single character.
import pandas as pd
df = pd.DataFrame(
{'Phone':['(555)123-1234','(555)555-1234','(555)545-1234','(555)888-1234']}
)
df['NewPhone'] = df['Phone'].str.replace('[()-]','', regex=True)
print(df)
Output from df
Phone NewPhone
0 (555)123-1234 5551231234
1 (555)555-1234 5555551234
2 (555)545-1234 5555451234
3 (555)888-1234 5558881234