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?

Johnson
  • 85
  • 1
  • 7

3 Answers3

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.

PeptideWitch
  • 2,239
  • 14
  • 30
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)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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())
Bishal Udash
  • 142
  • 2
  • 8