I am trying to update DataFrame column names if a substring exists within the column name, however I only need to update the substring, retaining all information either side of it.
Example:
import pandas as pd
lookup = [('abc_param_one_new', 'param_one'),
('abc_param_two_new', 'param_two'),
('abc_param_three_new', 'param_three')]
d = {'p1_abc_param_ONE_new1': [1,2],
'p2_abc_param_one_new2': [45,3],
'p3_abc_Param_One_new3': [76,5],
'p4_abc_param_two_new1': [4321,6],
'p5_abc_param_Two_new2': [3,2],
'p6_abc_param_THREE_new1': [6,5]}
df = pd.DataFrame(d)
# make all lowercase
df.columns = [x.lower() for x in df.columns]
I am trying to use the lookup var (this could be a different format though) to search and replace the substring within each column name.
e.g.
'p1_abc_param_ONE_new1' -> 'p1_param_one1'
I can do this primitively looping over the lookup and the checking for each column header, but can't manage to replace the substring.
Additionally it feels that the nested loop approach is not very efficient. Is there a more pythonic way to do this perhaps using built in pandas methods and/or list comprehensions?