简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM