1

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

4
  • what happens when you print(key) ? Commented May 15, 2019 at 18:48
  • You have never defined Filter Commented May 15, 2019 at 19:18
  • 1
    Simply use a dict of data frames and not separate named objects. Commented May 15, 2019 at 21:10
  • When I print(key) within the for loop, it prints all the dataframes one by one as the loop iterates. If I print it outside the for loop, it prints the last dataframe in the for loop. I am using spyder from Anaconda to do this. Commented May 16, 2019 at 6:56

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Or use dictionary comprehension: df_dict = {key: pd.read_csv(value) for key, value in files_dict.items()}
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.