Home > Enterprise >  Replacing multiple special characters on dataframe
Replacing multiple special characters on dataframe

Time:07-02

I have dataframe with 80 columns and column names has special characters like colon, Ampersand, Slash, Minus, space and Parenthesis.

Currently, I'm doing multiple replace on df.columns, my code as follows:

df.columns = df.columns.str.replace(' ','').str.replace('-','').str.replace('&','').str.replace('/','').str.replace(':','').str.replace('(','').str.replace(')','')

Is there any betters ways to replace all special characters at once on dataframe column names.

CodePudding user response:

You can use a regex:

df.columns = df.columns.str.replace(r'[ \-&/:()]', '', regex=True)

Some characters need to be escaped in regexes, so to be safe you can get re to do it for you:

import re
regex = '[' re.escape(r' -&/:()') ']'
# '[\\ \\-\\&/:\\(\\)]'
df.columns = df.columns.str.replace(regex, '', regex=True)
  • Related