0

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.

2 Answers 2

3

You want to use a dict instead of a list to store the DataFrames, if you need to somehow access their "names" and manipulate them programmatically (think when you have thousands of them). Also note the use of the inplace argument, which is common in pandas:

import pandas as pd col = ['open', 'high', 'low', 'close'] index = [1, 2, 3, 4] df_all = {'df1': pd.DataFrame(columns=col, index=index), 'df2': pd.DataFrame(columns=col, index=index), 'df3': pd.DataFrame(columns=col, index=index)} for key, df in df_all.iteritems(): df.rename(columns=lambda x: key+x, inplace=True) print df_all['df1'].columns.tolist() 

Output:

['df1open', 'df1high', 'df1low', 'df1close'] 
Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of issues here. Firstly, dflist is the list of DataFrames, as opposed to the names of those DataFrames. So df1 is not the same as "df1", which means that key + x isn't a string concatenation.

Secondly, the rename() function returns a new DataFrame. So you have to pass the inplace=True parameter to overwrite the existing column names.

Try this instead:

dflist = ['df1', 'df2', 'df3'] for key in dflist: df = eval(key) df.rename(columns=lambda x: key+x, inplace=True) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.