2

I have a data frame with different columns name (asset.new, few, value.issue, etc). And I want to change some characters or symbols in the name of columns. I can do it in this form:

df.columns = df.columns.str.replace('.', '_') 
df.columns = df.columns.str.replace('few', 'LOW') 
df.columns = df.columns.str.replace('value', 'PRICE') 
....

But I think it should have a better and shorter way.

0

2 Answers 2

2

You can create a dictionary with the actual character as a key and the replacement as a value and then you iterate through your dictionary:

df = pd.DataFrame({'asset.new':[1,2,3],
                   'few':[4,5,6],
                   'value.issue':[7,8,9]})
replaceDict = { '.':'_', 'few':'LOW', 'value':'PRICE'}
for k, v in replaceDict.items():
    df.columns = [c.replace(k, v) for c in list(df.columns)]
    
print(df)

output:

asset_new  LOW  PRICE_issue
       1    4            7
       2    5            8
       3    6            9

or:

df.columns = df.columns.to_series().replace(["\.","few","value"],['_','LOW','PRICE'],regex=True)

Produces the same output.

Sign up to request clarification or add additional context in comments.

Comments

0

Use Series.replace with dictionary - also necessary escape . because special regex character:

d = { '\.':'_', 'few':'LOW', 'value':'PRICE'}

df.columns = df.columns.to_series().replace(d, regex=True)

More general solution with re.esape:

import re

d = { '.':'_', 'few':'LOW', 'value':'PRICE'}
d1 = {re.escepe(k): v for k, v in d.items()}    
df.columns = df.columns.to_series().replace(d1, regex=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.