0

i have the following ;

    X=["A", "B" ,"C"]
    import pandas as pd    
    dfX = pd.read_csv(r"C:\HSTS\OB\ODO\X\*.csv", delimiter=';')

I would like to create a loop such that the dataframe is created based on the csv file in the "X" Folder so that final data frames can be

   dfA = pd.read_csv(r"C:\HSTS\OB\ODO\A\test.csv", delimiter=';')
   dfB = pd.read_csv(r"C:\HSTS\OB\ODO\B\test.csv", delimiter=';')
   dfC = pd.read_csv(r"C:\HSTS\OB\ODO\C\test.csv", delimiter=';')

the loop would be something like; for name in X: df{name} = pd.read_csv(r"C:\HSTS\OB\ODO{name}*.csv", delimiter=';')

I am having issues with the syntax

Kamikaze K
  • 181
  • 1
  • 11
  • The answer already given is recommended, but you can also use this method to create multiple variables instead of the dictionary format. `X=["A", "B" ,"C"];for x in X:exec('df{} = pd.read_csv(r"C:\HSTS\OB\ODO\{}\test.csv", delimiter=";")'.format(x,x))` – r-beginners Aug 01 '21 at 03:15

1 Answers1

1

Use a container to store your dataframes, like a dictionary:

my_dfs = {}
for x in ['A', 'B', 'C']:
    my_dfs[x] = pd.read_csv(r"C:\HSTS\OB\ODO\%s\test.csv" % x, delimiter=';')

Then access the dataframes per key:

my_dfs['A']

It is a much better practice than having many variables floating around. You can then easily access your dataframes programmatically for downstream processing.

mozway
  • 194,879
  • 13
  • 39
  • 75