0

How do I read different csv files in folder without concatenating them but just assigning them with the original file name? For example, file with path ...\table1.csv will be named "table1"

I managed to get all file names how do I read each file now?

from os import listdir
from os.path import isfile, join

mypath= ...
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

In other words, instead of reading multiple csv files in pandas like this:

table1 = pd.read_csv(r'C:\Users\username\Folder\Desktop\FolderA\FolderB\Sub_Folder\OneDrive_1_22-06-2022\table1.csv')

table2 = pd.read_csv(r'C:\Users\username\Folder\Desktop\FolderA\FolderB\Sub_Folder\OneDrive_1_22-06-2022\table2.csv')

table3 = pd.read_csv(r'C:\Users\username\Folder\Desktop\FolderA\FolderB\Sub_Folder\OneDrive_1_22-06-2022\table3.csv')
...

is there a better way?

3
  • 1
    can you use a dictionary? Commented Jun 22, 2022 at 5:09
  • 1
    Does this answer your question? How loop and store values in independent variable in python Commented Jun 22, 2022 at 5:14
  • 1
    I don't think declaring variables from external inputs is a good practice, sure you can do it with eval or exec. But this makes the program unpredictable and vulnerable, for almost most of the time. Commented Jun 22, 2022 at 5:18

3 Answers 3

3

Use pathlib and dictionary:

import pandas as pd
import pathlib

dfs = {f.stem: pd.read_csv(f) for f in pathlib.Path().glob('*.csv')}

Strongly discouraged, prefer method above

If you want to create variables dynamically:

for name, df in dfs.items():
    locals()[name] = df
    # locals()[f"df_{name}"] = df

Output:

>>> data1
   0:00  0:30
0     1     5
1     2     6
2     3     7
3     4     8
Sign up to request clarification or add additional context in comments.

8 Comments

Hi Corralien, thanks for your solution, instead of getting data in a whole chunk, can I assign them with different names and pandas dataframe?
What do you mean? Is it not already the case here: one file, one variable?
It's working now! Sorry I missed out the second bit - may I know it's discouraged?
For example, your file names need to be valid python identifier. data 1.csv is not a valid python identifier. If you search files recursively, you can have 2 files with same name and override a variable previously defined (but this is the same problem for a dict :))
May I ask what f.stem: pd.read_csv(f) does?
|
1
dfs = {file.split('.')[0]: pd.read_csv(file) for file in onlyfiles}
print(dfs['table1'])
...
<Your dataframe here>

Comments

1

let's try:

for file in onlyfiles:
    # get file name
    fname = file.split('.')[0]

    # read dataframe with file name as variable name
    exec('{} = pd.read_csv(file)'.format(fname))

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.