cyrus24
cyrus24

Reputation: 363

Read multiple csv files starting with a string into separate data frames in python

I have about 500 '.csv' files starting with letter 'T' e.g. 'T50, T51, T52 ..... T550' and there are some other ',csv' files with other random names in the folder. I want to read all csv files starting with "T" and store them in separate dataframes: 't50, t51, t52... etc.'

The code I have written just reads these files into a dataframe

import glob
import pandas as pd

for file in glob.glob("T*.csv"):
    print (file)

I want to have a different name for each dataframe - preferably, their own file names. How can I achieve this within its 'for loop'?

Upvotes: 2

Views: 4022

Answers (2)

ABot
ABot

Reputation: 197

Totally agree with @Comos
But if you still need individual variable names, I adapted the solution from here!

import pandas as pd
import os

folder = '/path/to/my/inputfolder'

filelist = [file for file in os.listdir(folder) if file.startswith('T')]
for file in filelist:
    exec("%s = pd.read_csv('%s')" % (file.split('.')[0], os.path.join(folder,file)))

Upvotes: 3

Comos
Comos

Reputation: 82

In additions to ABotros's answer, to read all files in different dataframes, I would recommend adding the files to a dictionary, which will allow you to save dataframes with different names in a loop:

filelist = [file for file in os.listdir(folder) if file.startswith('T')]

database = {}
for file in filelist:
    database[file] = pd.read_csv(file)

Upvotes: 2

Related Questions