0

I have a folder full of txt files and I want to import each as their own df. I want to call each df the same name as the txt file.

This is what I have to read in all files:

import pandas as pd
import glob

path =r'D:\Path\to\ files' 
allFiles = glob.glob(path + "/*.txt")
for file_ in allFiles:
  df  = pd.read_csv(file_,index_col=None, header=0)

Instead of each df being called df I would like it to be the *.txt filename.

martineau
  • 119,623
  • 25
  • 170
  • 301
Yolo_chicken
  • 1,221
  • 2
  • 12
  • 24
  • 1
    Why don't you add every `.txt` to a dict of dataframes `df_map` where the name of the `.txt` file maps to the df itself. You should not have variable variable names – quantik Mar 13 '18 at 18:54

2 Answers2

1

One option would be to create a dictionary and access each as you would a normal array.

for f in files:
    dfs.append(pd.read_csv(f, index_col=None, header=0))
# use dfs[0]

Alternatively, if you know each filename is unique, create a dictionary mapping file names to dataframes

dfs = {}
for f in files:
    dfs[f] = pd.read_csv(f, index_col=None, header=0))
# do stuff to dfs['filename']

The most pythonic way however would be a dictionary comprehension

dfs = { f: pd.read_csv(f, index_col=None, header=0) for f in all_files }

Note, I think I changed your variable names to a more pythonic convention. So either remember to change my variable names back to yours or conform!

Skam
  • 7,298
  • 4
  • 22
  • 31
0

You could achieve what you want with exec as stated in the first answer here Using a string variable as a variable name, but thats not a good solution, I recommend a dictionary

import pandas as pd
import glob

path =r'D:\Path\to\ files' 
allFiles = glob.glob(path + "/*.txt")
df_dict = {}
for file_ in allFiles:
    df_dict[_file] = pd.read_csv(file_,index_col=None, header=0)
Dalvenjia
  • 1,953
  • 1
  • 12
  • 16