'Replace Multiple Values of columns

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.



Solution 1:[1]

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.

Solution 2:[2]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2