1

I've seen a few answers on reading multiple csv files into separate Pandas dataframes, and am still running into trouble. I've read my csv files and file names into a dictionary:

path = os.getcwd()
file_names = ['file1', 'thisisanotherfile', 'file3']

df_dict = {x: pd.read_csv('{}/{}.csv'.format(path, x)) for x in file_names}

Which seems to work: print(df_dict['file1'])

However what I'm looking for is a Pandas dataframe called 'file1' where I can access the data.

Is it possible to get this information from the dictionary? Do I have to call the dictionary in my code every time I want to access the data?

1

3 Answers 3

1

It wouldn't be efficient to convert them to variables but if you have to, do:

locals().update(df_dict)

Inside a function do:

def f():
    ...
    globals().update(df_dict)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is what I was looking for
1
frame = list(df_dict.values())

That should do the trick (as per this answer)!

Explanation: dictionary values returned with the df.values() call are what's called a 'view' - this is sort of like a shorthand response, but it's not actually the proper stored value. This is done for efficiency's sake so that the user can preview the value before accessing it. list(df.values()), then, actually converts the dictionary key's value into a usable form - in this case, your dataframes.

Comments

0

Try this :

import pandas as pd
import os

# get folder path
folder_path = os.getcwd()
file_names = ['Siddhartha', 'employee_file2']

for file in file_names:
    final_df = file+"_df"
    print("Dataframe name : "+final_df)

    filename = file+".csv"
    final_df = pd.read_csv(filename)
    print(final_df.head())

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.