简体   繁体   中英

Reading multiple CSV files with different names using python dictionary in a for loop

I have a list of filenames and filepaths that I have stored in a python dictionary. I am trying to read the files using pandas' read_csv and assign the dataframe names from the dictionary. I can read and print the dataframes while running the for loop but I cannot call the dataframes after the loop is finished. I could append all the dataframes in a list but that way I am not able to assign these dataframes different names which are also stored in the dictionary.

I checked various forums but none of them explain why the for loop with pd.read_csv doesn't work and why I am not able to assign the names to the dataframes in the for loop to later use them.

import pandas as pd
files_dict = {"Filter":"OUTTYPENM.csv","previous":"previous.csv"}
for key, value in files_dict.items():
    key = pd.read_csv(value)
Filter.head()

I expect to see the first five lines from the Filter dataframe as if I have read the dataframe as following.

Filter = pd.read_csv("OUTTYPENM.csv")

All the csv files are in the current working directory.

When I run the for loop code, and run the Filter.head(), I get an error saying - NameError: name 'Filter' is not defined

This doesn't exactly answer your question, but I think it gets you to a similar place, without involving any exec() or locals() calls.

Instead of creating variables named after your dictionary keys, you can just have a second dictionary where the keys are the same and the values are now the DFs you read in.

import pandas as pd
files_dict = {"Filter":"OUTTYPENM.csv","previous":"previous.csv"}
df_dict = {}
for key, value in files_dict.items():
    df_dict[key] = pd.read_csv(value)
df_dict['Filter'].head()

Try this:

for key, value in files_dict.items():
    locals()[key] = pd.read_csv(value)

This method is not recommended though. See the link here: https://www.linuxquestions.org/questions/programming-9/python-create-variables-from-dictionary-keys-859776/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM