1

I have a folder containing 3 csv files:

a.csv
b.csv
c.csv

To read all the csv's in this folder and create a dataframe, I'm currently doing this:

df1 = pd.read_csv('a.csv')
df2 = pd.read_csv('b.csv')
df3 = pd.read_csv('c.csv')

Is there any way to automate the naming of the dataframes (df1, df2 and df3) and reading of all the csv files in that folder. Say, I have 10 csv files, I don't want to manually write 10 read statements in pandas.

For example, I don't want to write this:

df1 = pd.read_csv('a.csv')
......
......
......

df10 = pd.read_csv('j.csv')

Thanks!

1
  • see this question, and this. Commented Jun 16, 2017 at 21:48

3 Answers 3

2

You can do this quite easily if you're willing to access a list of dataframes rather than have df1...dfn explicitly declared:

root= "YOUR FOLDER"
csvs= []  #container for the various csvs contained in the directory
dfs = []  #container for temporary dataframes

# collect csv filenames and paths 
for dirpath, dirnames, filenames in os.walk(root):
    for file in filenames:
        csvs.append(dirpath + '\\' + file)

# store each dataframe in the list
for f in csvs:
    dfs.append(pd.read_csv(f))    

Then access like dfs[0] ... dfs[n]

Sign up to request clarification or add additional context in comments.

1 Comment

In the OP's code he knows that df1 corresponds to the file named 'a.csv'. If that's important I suppose op could make dfs a dictionary and add them by doing dfs[f] = read_csv(f).
1

You can create a dictionary of DataFrames:

import os
import pandas as pd
from glob import glob

dfs = {os.path.splitext(os.path.basename(f))[0]: pd.read_csv(f) for f in glob('*.csv')}
# df1 equivalent dfs['a'] 
dfs['a']

Comments

0

People may downvote this solution since I am asking you to play with global variables. But, this solves your problem.

dir= 'myDir'
for root, dirs, filenames in os.walk(dir):
    for a, f in enumerate(filenames):
        fullpath = os.path.join(dir, f)
        globals()['df%s' % str(a+1)] = pd.read_csv(fullpath)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.