Tara
Tara

Reputation: 13

removing special characters from a column in pandas dataframe

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

Answers (1)

mozway
mozway

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

Related Questions