I am looping through Excel worksheets and appending them to a list. When the loop finishes, I use Pandas to concat to a single dataframe. The problem I'm having is adding the worksheet name into the appropriate list.
# infile is a filepath variable
xls = xlrd.open_workbook(infile, on_demand=True)
dfList = []
for sheet_name in xls.sheet_names():
df = pd.read_excel(infile, sheet_name, header = 0)
#df['Well_name'] = sheet_name
dfList.append(df)
print(sheet_name + " appended.")
#time.sleep(2)
print("Loop complete")
# Concatenating the appended lists
dfs = pd.concat(dfList, axis=0)
I tried creating a new column in df but that created a length mismatch and it also didn't work because it was constantly rewritten to the last worksheet name in the loop.
Any thoughts or suggestions?