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

1
  • 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)) Commented Aug 1, 2021 at 3:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.