I am trying to combine multiple sheets from different excel files into one dataframe. All the files have multiple sheets, and one of these sheets has the same name in all the files - this is the sheet that I am interested in combining into one dataframe. All the files are in the same directory.
import pandas as pd
import os, glob
os.chdir(r'c:\Users\Documents\Files')
def files(): #to select the files that have RMP and WE on their name
list_files= pd.Series()
for file in glob.glob('RMP*WE*'):
data= pd.Series(file)
list_files= list_files.append(data, ignore_index=True)
return list_files
a= files()
print("This is the variable a\n", a)
def extract_tab(): #to concatenate the sheet called Metrics that all files have
frame_files= pd.DataFrame()
try:
for file in a:
data= pd.read_excel(file,sheet_name='Metrics')
frame_files= frame_files.append(data, ignore_index=True)
except:
pass
return frame_files
b= extract_tab()
print("This is b\n",b)
The result of variable a (files function) is a list of the files that meet the naming criteria. But the result of variable b (extract_tab function) is an empty dataframe. What am I doing wrong?
I looked at this postImport multiple excel files into python pandas and concatenate them into one dataframe but it doesn't work... although I did take a couple of ideas from it.