Home > Software engineering >  removing special characters from a column in pandas dataframe
removing special characters from a column in pandas dataframe

Time:07-01

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 no luck.. any idea how i can resolve this issue? thank you!

CodePudding user response:

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
  • Related