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?

roudan
  • 3,082
  • 5
  • 31
  • 72
  • 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 https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables – MattDMo Jan 04 '22 at 21:48

1 Answers1

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)
MattDMo
  • 100,794
  • 21
  • 241
  • 231