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?!
CodePudding user response:
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']