Renaming Substring In Column Names With Dictionary
I hope someone can help. I need to replace part of all column names present in a dataframe using a dictionary shown below. I need to replace the first part (e.g. 'z987') with the r
Solution 1:
You can do
df1.columns = [y.replace(x, dic[x]) for x in dic.keys() for y in df.columns if x in y]
Essentially, you can iterate over the keys of dictionary and the columns of dataframe and replace the substring with value of the dictionary. Now you can reassign the columns of df1
Post a Comment for "Renaming Substring In Column Names With Dictionary"