Deepak
Deepak

Reputation: 490

Replacing multiple special characters on dataframe

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.

Upvotes: 1

Views: 669

Answers (1)

mozway
mozway

Reputation: 260300

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)

Upvotes: 1

Related Questions