Skip to content
Advertisement

Read multiple csv files with Pandas assign different names

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:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table1.csv')

table2 = pd.read_csv(r'C:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table2.csv')

table3 = pd.read_csv(r'C:UsersusernameFolderDesktopFolderAFolderBSub_FolderOneDrive_1_22-06-2022table3.csv')
...

is there a better way?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement