Jonas Palačionis
Jonas Palačionis

Reputation: 4842

Creating dataframes from each .csv in a folder after grabbing important part of the filename

I have a folder that has 50 .csv files.

They are named UselessInfo.Needed_info.csv or UselessInfo.Needed.csv

I create a list of the names I want to assign as a variable to the representing df:

files = []
for filename in os.listdir():
    if not filename.endswith('.ipynb'):
        if '_' in filename or len(filename.split('.')) > 2:
            files.append(filename.split('.')[1])
        else:
             files.append(filename.split('.')[0])

Then I iterate over the files and create a df for each file:

for filename in files:
    filename = pd.read_csv(UselessInfo.{filename}.csv', delimiter=';')

But none of the names in files are created as a df, only filename holds the last file's data. Why the iteration of files does not create a df with the name in the files held by the filename variable?

I've seen this answer:

path = Path(os.getcwd()) 

dict_of_df = {}
for file in path.glob('*.csv'):
    dict_of_df[file.stem] = pd.read_csv(file,delimiter=';')

Which creates a dict of df's but how can I achieve so that I create a variable for each file in the folder?

Upvotes: 0

Views: 121

Answers (1)

Z Li
Z Li

Reputation: 4318

updated to address op's goal: get variables named just like filename pointing to each df.

This is a bad idea compared to putting them in a dict. But if you insist here is how to do it:

for filename in files:
    globals()[filename] = pd.read_csv(UselessInfo.{filename}.csv', delimiter=';')

This would create a variable using filename (a str).

Reference: Using a string variable as a variable name [duplicate]

Upvotes: 1

Related Questions