Gaurav Bansal
Gaurav Bansal

Reputation: 5660

Read in multiple csv into separate dataframes in Pandas

I have a long list of csv files that I want to read as dataframes and name them by their file name. For example, I want to read in the file status.csv and assign its dataframe the name status. Is there a way I can efficiently do this using Pandas?

Looking at this, I still have to write the name of each csv in my loop. I want to avoid that.

Looking at this, that allows me to read multiple csv into one dataframe instead of many.

Upvotes: 3

Views: 9598

Answers (2)

knh190
knh190

Reputation: 2882

You can list all csv under a directory using os.listdir(dirname) and combine it with os.path.basename to parse the file name.

import os

# current directory csv files
csvs = [x for x in os.listdir('.') if x.endswith('.csv')]
# stats.csv -> stats
fns = [os.path.splitext(os.path.basename(x))[0] for x in csvs]

d = {}
for i in range(len(fns)):
    d[fns[i]] = pd.read_csv(csvs[i])

Upvotes: 9

lorenzori
lorenzori

Reputation: 747

you could create a dictionary of DataFrames:

d = {}  # dictionary that will hold them 

for file_name in list_of_csvs:  # loop over files

   # read csv into a dataframe and add it to dict with file_name as it key
   d[file_name] = pd.read_csv(file_name)


Upvotes: 1

Related Questions