1

So I have multiple participants with 3 trials each. All my files have the same naming convention so I have used a for loop to quickly read all the files. I need to now assign each file to its own dataframe that is easy to read and allows me work on as many as possible at a time.

currently I have

for p in range(participants):
    for t in range(tests):
       filename = 'P'+str(p+1)+'S'+str(t+1)+'.csv'

I now need to assign the files to a data frame that would have a name of df(p+1)(t+1) i.e df11,df12,df13,df21, ...df153

I will need to access multiple data frames at a time and not sure how to achieve all of these data frames in an efficient manner.

I'd appreciate it if anyone could tell me how to achieve this

Adon Alves
  • 13
  • 2

1 Answers1

0

Creating variable variables is a bad idea in Python. Instead, lists and dictionaries accomplish the same thing, so I would recommend storing your DataFrames in a dictionary with the naming scheme you have:

df_dict = {}
for p in range(participants):
    for t in range(tests):
        key_name = 'df' + str(p+1) + str(t+1)
        df_dict[key_name] = pd.read_csv('P'+str(p+1)+'S'+str(t+1)+'.csv')

Then you can access each DataFrame through the dictionary key: df_dict['df11']

Derek O
  • 16,770
  • 4
  • 24
  • 43