Johnson
Johnson

Reputation: 95

Read multiple csv files into separate pandas dataframes

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?

Upvotes: 1

Views: 2082

Answers (3)

Bishal Udash
Bishal Udash

Reputation: 182

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())

Upvotes: 0

PeptideWitch
PeptideWitch

Reputation: 2349

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.

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71560

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)

Upvotes: 1

Related Questions