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.
Thanks for help
>Solution :
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)