I have a folder with any number of .csv files. For all intents and purposes let's say I have three now: map1.csv, map2.csv and map3.csv.
I want to import the contents of these .csv files into a dataframe with a name that is the same as the filename they came from. So the content of map1.csv in folder ### needs to be in a dataframe named map1 (or map1.csv).
I know creating variables dynamically is not the best thing, but it is what I need exactly. So if there are better ways to tackle my problem I'd be happy to learn.
Thanks in advance :)
Here's what I tried: filenames obviously contains the names of the files csv_files contains the paths to those files count is number of files in the folder path is the path of the folder which contains the files
import pandas as pd
from tkinter.filedialog import askdirectory
import fnmatch
import os
import glob
path = askdirectory(title='Select Folder') # shows dialog box and return the path
count = int(len(fnmatch.filter(os.listdir(path), '*.*')))
filenames = os.listdir(path)
csv_files = glob.glob(os.path.join(path, "*.csv"))
for i in range(count):
var_name = filenames[i]
value = pd.read_csv(csv_files[i])
globals()[var_name]= value
print(Map1.csv)