I have a list with lots of dataframes
col = ['open', 'high', 'low', 'close'] index = [1, 2, 3, 4] df1 = pd.DataFrame(columns=col, index=index) df2 = pd.DataFrame(columns=col, index=index) df3 = pd.DataFrame(columns=col, index=index) dflist = [df1, df2, df3] I need to rename all the columns of all the dataframes in the list. I need to add the name of each dataframe to the name of each column. I tried to do it with a for loop.
for key in dflist: key.rename(columns=lambda x: key+x) Obviously, this is not working. The desired output would be:
In [1]: df1.columns.tolist() Out [2]: ['df1open', 'df1high', 'df1low', 'df1close'] In [3]: df2.columns.tolist() Out [4]: ['df2open', 'df2high', 'df2low', 'df2close'] In [5]: df3.columns.tolist() Out [6]: ['df3open', 'df3high', 'df3low', 'df3close'] Thanks for your help.