0

I want to open multiple csv files with for loop.

For example, the file names are 0.csv 1.csv 2.csv ... 80.csv

and i want to read those files in python with df0 df1 df2 ... df80

what i have tried was

for i in range(0,81):
    df+"{0}".format(i)= pd.read_csv("C:/Users/{0}.csv".format(i), engine='python')

i am quite confident about using {0} inside the file address. but i am not sure what to use to make file names df0, df1, df2

please help! if {0} use inside the address is also wrong please correct.

  • 3
    Do you **really** want to make the variable names like that? You should just store them into a list instead. (df[0], df[1], etc.) – user202729 Jan 06 '21 at 04:33
  • 1
    (if you really do, there's still https://stackoverflow.com/questions/10806327/python-create-a-global-variable-from-a-string and https://stackoverflow.com/questions/8028708/dynamically-set-local-variable . Otherwise just create a list normally) – user202729 Jan 06 '21 at 04:35
  • the problem is df is not defined at first, and each csv file has multiple variables, so i thought making separate csv files df0 df1 df2 was more convenient – MinGeun Song Jan 06 '21 at 04:37
  • The list can be created easily such as `df_list = [pd.read_csv("C:/Users/{0}.csv".format(i)), engine='python') for i in range(0,81)]`. This way, df_list will contain `[df[0], df[1], ...]`. – DarrylG Jan 06 '21 at 04:54

0 Answers0