Home > Back-end >  Read multiple csv files with Pandas assign different names
Read multiple csv files with Pandas assign different names

Time:06-22

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?

CodePudding user response:

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

CodePudding user response:

dfs = {file.split('.')[0]: pd.read_csv(file) for file in onlyfiles}
print(dfs['table1'])
...
<Your dataframe here>

CodePudding user response:

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))
  • Related