0

I have multiple csv files, named "csv_i", where i varies from 1 to 54. I want to create a for loop to read each one of these csv files in a similarly named dataframe (e.g. df_i), as such:

it = np.arange(1,55)
for i in it:
    my_csv = ('csv_{}.csv'.format(i))
    df_i = pd.read_csv(my_csv)

Can you help me with the last line of the code please? I cannot use something like: "df_{}.format(i) = pd.read_csv(my_csv), so I was wondering how I can achieve it.

Please note, that I saw somewhere that you can create a dictionary and each pair of key-values will be each one of the 54 df, but I'd prefer to have 54 separate dfs instead. Thanks

ALollz
  • 57,915
  • 7
  • 66
  • 89
Dsto4
  • 103
  • 3
  • 10
  • Check this post on SO, this answers different ways in which it can be done. https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Sumit S Chawla Jun 14 '18 at 04:56
  • i think this post could help you [Read multiple csvs into multiple dataframes in Pandas](https://stackoverflow.com/questions/42934137/read-multiple-csvs-into-multiple-dataframes-in-pandas) – nimrodz Jun 14 '18 at 05:21

0 Answers0