0

I have dataframe's name like this df_1,df_2, df_3... df_10, now I am creating a loop from 1 to 10 and each loop refers to different dataframe which name is df+ loop name

name=['1','2','3','4','5','6','7','8','9','10']
for na in name:
  data=f'df_{na}'.iloc[:,0]

if I do like above, I got an error of AttributeError: 'str' object has no attribute 'loc'

so I need to convert the string into dataframe's name

how to do it?

1
  • 1
    Put your dataframes in a list and iterate over the list. In the future, don't create multiple dataframes like that. Use a dictionary instead. See stackoverflow.com/questions/1373164/… Commented Jan 4, 2022 at 21:48

1 Answer 1

2

Based on our chat, you're trying to make 100 copies of a single dataframe. Since making variable variables is bad, use a dict instead:

names = ["df_" + int(i) for i in range(1, 101)]
dataframes = {name: df.copy() for name in names} # df is the existing dataframe
for key, dataframe in dataframes.items():
    temp_data = dataframe.iloc(:, 0)
    do_something(temp_data)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Matt, I appreciate your help today

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.