0

for professional purposes I need to produce some reports that includes new entries every week. I have 16 dataframes having same column names (df names are week1, week2... week16).

I created a list of the dataframes and then a loop. I wanted to test rename of column with index = 1 and I did not succeed.

[35] lists = [week1, week2, week3, week4, week5, week6, week7, week8, week9, week10,
           week11, week12, week13, week14, week15, week16]

[36] for i in lists:
    i.rename(columns={'1':'State'},inplace=True)

I am forced to manually change every column name because I can't set up the loop. Besides this is only one of the columns.

How can I make sure I can call all dataframes in a loop?

I have tried also the suggestions in this thread but somehow it did not work to me to use the append method. Indeed, the column names aren't edited after I run the script.

Thanks for the help!

1 Answer 1

1

Sample dataframes:

#import numpy as np
#import pandas as pd
df1 = pd.DataFrame(np.random.randn(10,10),columns=range(10))
df2 = pd.DataFrame(np.random.randn(10,10),columns=range(10))
df3 = pd.DataFrame(np.random.randn(10,10),columns=range(10))
df_list = [df1, df2, df3]

Try via list comprehension and rename() method:

df_list =[x.rename(columns={1:'State'}) for x in df_list]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.