简体   繁体   中英

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?

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.

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)

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

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