Rafael Higa
Rafael Higa

Reputation: 725

Renaming dataframe columns with regex and dictionary

I have a dataframe like this:

code_0101    code_0102    code_0103    code_0104    ...
0            1            2            3            ...
...          ...          ...          ...

I also have a dictionary:

{'0101': 'cirurgical_procedures', '0102': 'medical_care', '0103': 'remedy', ...}

I want to apply a regex to dataframe column and replace the code numbers accorging to my dictionary, and get something like this:

code_cirurgical_procedures    code_medical_care       ...
0                             1                       ...
...                           ...

How can I do these two steps, specially the second, about the dictionary?

Upvotes: 0

Views: 350

Answers (2)

Andy L.
Andy L.

Reputation: 25259

Use series.replace to replace and assign back to columns

d = {'0101': 'cirurgical_procedures', '0102': 'medical_care', '0103': 'remedy'}
df.columns = df.columns.to_series().replace(d, regex=True)

Out[12]:
   code_cirurgical_procedures  code_medical_care  code_remedy  code_0104
0                           0                  1            2          3

Upvotes: 3

sushanth
sushanth

Reputation: 8302

Assuming code as perfix you can use a dict comprehension,

 cols = {'0101': 'cirurgical_procedures', '0102': 'medical_care', '0103': 'remedy'}

df.rename(columns = {f"code_{k}": f"code_{v}" for k,v in cols.items()})

Upvotes: 1

Related Questions