Reputation: 13
I have characters such as " ' . , " in a column in my df, and i cannot remove them using the replace() function. I have tried the following
df.column_name = df.column_name.replace('/[^a-zA-Z ]/g', '')
But the result still has ' in the name.
Example:
df:
id column_name
0 aaa sam
1 bbb joe's
Result after running the code:
df.column_name = df.column_name.replace('/[^a-zA-Z ]/g', '')
df.head(2)
df:
id column_name
0 aaa sam
1 bbb joe's
I also tried it specifically for ' character by running:
df.column_name = df.column_name.replace("'", '')
But doesn't work. Any idea how I can resolve this issue?
Upvotes: 1
Views: 1442
Reputation: 260300
Remove the /
and g
, this syntax is not valid in python:
df['column_name'] = df['column_name'].str.replace(r'[^a-zA-Z ]', '', regex=True)
output:
id column_name
0 aaa sam
1 bbb joes
Upvotes: 1