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?
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
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()})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.