-1

I have a folder that contains a few excel files. I want to read through the folder and save each excel file in a separate dataframe with a similar name using pandas. Currently, I extracted the file names and made a dictionary with keys as dataframe names and values as excel file addresses but I'm not sure how I can assign them in iterations. For example, in the first step all file names and paths are red and saved in 2 lists:

path='c:/files/*.xlsx'
path_1='c:/files/'
file_names = [os.path.basename(i) for i in glob.glob(path)]
names = ['df_'+i.split('.')[0]+ for i in file_names]
d_names={names[i]:path_1+file_names[i] for i in range(len(names))}

So now there is a dictionary as below:

d_names={'df_a':'c:/files/a.xlsx','df_b':'c:/files/b.xlsx'}

Now, how I assign two data frames iteratively so that at the end of iteration we obtain two data frames as df_a and df_b?!

Mohammad.sh
  • 245
  • 1
  • 10
  • Are you trying to generate the variables `df_a` and `df_b` based on the file names? If so, that is a terrible idea. A Dictionary of DataFrames would be far better in this case. – BeRT2me Aug 27 '22 at 01:15
  • Yes. Would you please explain the reason for that? – Mohammad.sh Aug 27 '22 at 04:49
  • The only way is to use functions that are generally frowned upon. https://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided – BeRT2me Aug 27 '22 at 22:23

1 Answers1

0
path='c:/files/'
dfs = {}
for file in glob.glob(path + '*.xlsx'):
     file_name = os.path.basename(file).split('.')[0]
     dfs['df_'+file_name] = pd.read_excel(file)

dfs would now contain:

{'df_a': <pd.DataFrame>, 'df_b': <pd.DataFrame>}

# And each could be accessed like:
dfs['df_a']
BeRT2me
  • 12,699
  • 2
  • 13
  • 31