Read multiple csv files into separate pandas dataframes

折月煮酒 提交于 2021-02-08 07:49:57

问题


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:


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)



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/57033461/read-multiple-csv-files-into-separate-pandas-dataframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!