5

Is there a way to read multiple csv files into Pandas through a loop and define them as such?

for i in ['a', 'b', 'c', 'd']:
    csv_(i) = pd.read_csv('C:/test_{}.csv'.format(i))

I see multiple questions about reading and appending multiple csvs into a single dataframe. Not the other way around.

cptpython
  • 393
  • 2
  • 5
  • 12

1 Answers1

8

You can use dict comprehension for dict of DataFrames:

dfs = {i: pd.read_csv('C:/test_{}.csv'.format(i)) for i in ['a', 'b', 'c', 'd']}

print (dfs['a'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252