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?
JavaScript
x
6
1
from os import listdir
2
from os.path import isfile, join
3
4
mypath=
5
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
6
In other words, instead of reading multiple csv files in pandas like this:
JavaScript
1
7
1
table1 = pd.read_csv(r'C:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table1.csv')
2
3
table2 = pd.read_csv(r'C:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table2.csv')
4
5
table3 = pd.read_csv(r'C:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table3.csv')
6
7
is there a better way?
Advertisement
Answer
Use pathlib
and dictionary:
JavaScript
1
5
1
import pandas as pd
2
import pathlib
3
4
dfs = {f.stem: pd.read_csv(f) for f in pathlib.Path().glob('*.csv')}
5
Strongly discouraged, prefer method above
If you want to create variables dynamically:
JavaScript
1
4
1
for name, df in dfs.items():
2
locals()[name] = df
3
# locals()[f"df_{name}"] = df
4
Output:
JavaScript
1
7
1
>>> data1
2
0:00 0:30
3
0 1 5
4
1 2 6
5
2 3 7
6
3 4 8
7