0

I have a list of .csvs - They have been named in a very uniform fashion, say, BMW_year i.e. BMW_60, BMW_61 ... BMW_70 ... BMW_00.. and so on. I want to pull them into a pandas dataframe - which I can do using a pd.read_csv(..) function.

But there are many .csvs and I was hoping to do something more on the lines of -

for i in range(70, 80):
     BMW_{i} = pd.read_csv(BMW_{i}.csv)

where {i} will be like a reference to whatever the value of i is during that iteration of the loop. Is there a way I can achieve this?

P.S. I saw a lot of questions on dynamic naming and people saying - use lists or dictionaries but I don't think thats what I am asking for, in this question.

Nilima
  • 197
  • 1
  • 2
  • 9
  • look here: https://stackoverflow.com/questions/19122345/convert-string-to-variable-name-in-python – magraf May 28 '21 at 15:53
  • @Ank -Because those questions are not like this. Those are simple user inputs, and not in a loop situation with just the 'suffix' of the variable name changing. My dataframe needs to be also changed into a panel dataset and dictionary and lists are very complicated. Also, I didn't see anyone changing the appending "number" to loop through variables which is something I need esp. since this will scale up – Nilima May 28 '21 at 16:08
  • @magraf - that was the first link I visited as well. But that doesn't cover how to use the iterator in a 'concatenated' fashion to name variables – Nilima May 28 '21 at 16:09
  • Check [this](https://stackoverflow.com/questions/67699830/pass-every-excel-file-in-python-from-assigning-a-specific-name) post. Had the same problem. – Ank May 28 '21 at 16:10
  • @ank - One of the major reasons I don't want a list of dataframes is for the fact that I need to 'panelize' them, use them individually and merge them and then other manipulations. Keeping a track of indexing that way - esp. when I will need to run multiple loops like this with different sets of csvs will be super hard. – Nilima May 28 '21 at 16:22

1 Answers1

1

have not testet yet, but I would try something like

for i in range(70, 80):
    fpath = 'BMW_%d.csv' %i
    vname = 'BMW_%d' %i
    exec("%s = pd.read_csv(%s)" % (vname, fpath))
magraf
  • 420
  • 5
  • 8
  • No :( I get an error `can't assign to operator` – Nilima May 28 '21 at 16:18
  • 1
    Then you probably have some weired file names. You can protect them with ' '. Als plug the integer directly in with `exec("BMW_%d = pd.read_csv( 'BMW_%d.csv')" % (i, i ))` – magraf May 28 '21 at 16:35
  • 1
    The take away message here is that you can build a string and execute it as code. So if woun't work, your string is no valid code. Also be aware that using your variables might be prohibited by some IDEs because you only define the names on runtime. – magraf May 28 '21 at 16:45
  • It worked :) Thanks. I really hope others who want a similar solution come here for your answer – Nilima May 28 '21 at 16:47
  • My folder name was weird - used os.chdir(..) to rectify it. – Nilima May 28 '21 at 16:48