Roberto Bertinetti
Roberto Bertinetti

Reputation: 615

Python: Create Dataframes with different names in Loop

I currently have a list of languages:

languages_list =['EN', 'DE', 'FR']

And a different CSV file for each language.

I would need to create a loop, where for each language I create a new dataframe by uploading the specific CSV file:

for language in languages_list:
tables_ + str{language} = pd.read_csv(language + ".csv", header = 0, encoding = "latin1")

The desired output should be 3x dataframe in this case:

  1. tables_EN, containing the EN csv
  2. tables_FR, containing the FR csv
  3. tables_DE, containing the DE csv

Is it possible to obtain an output as described?

Upvotes: 1

Views: 1661

Answers (1)

jezrael
jezrael

Reputation: 862406

I think better is create dictionary of DataFrames like:

tables={x: pd.read_csv(x + ".csv", header = 0, encoding = "latin1") for x in languages_list}

print (tables['EN'])

Upvotes: 3

Related Questions