简体   繁体   中英

how to read multiple CSV files from folder into pandas with dataframe name as file name

I have a folder which contain files like

  dm.csv
  ae.csv
  ex.csv

I want to read all file in one go without specifying file name into pandas with dataframe name as dm, ae, ex respectively.

I have tried different methods like use libraries

 dask.dataframe.read_csv
 glob.glob
 os.listdir

every method indicating concat of all dtaframes but i want individually

Now you cannot create variable names on fly. But what you can do is create a dictionary of dataframes with key as your file name.

from os import listdir
from os.path import isfile, join
import pandas as pd
mypath = 'folder path'
files = [f.split('.')[0] for f in listdir(mypath) if isfile(join(mypath, f))]

dataframe={}
for file in files:
     dataframe[file] = pd.read_csv(mypath+file+'.csv')#provide separator if required

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