I have an excel workbook with 3 worksheets, they are called "Z_scores", "Alpha" and "Rho" respectively.
In the future, this workbook will increase as the number of models and their corresponding parameters are stored here.
In my function I am looking to import each worksheet individually and save it to a dataframe, the name of the dataframe should be decided by the name of the worksheet.
So far, I have this function but I am not able to dynamically name the dataframe and I am unsure what should be written in the return statement
FYI: The import identifier function is simply a way of scanning in worksheet names and those with the identifier present should not be inserted e.g. putting a single blankspace at the beginning of the worksheet name will prevent the worksheet being imported.
#import libraries
import pandas as pd
#define function
def import_excel(filename, import_identifier):
#Create dataframe of the excel
df = pd.read_excel('Excel.xlsx')
# this will read the first sheet into df
xls = pd.ExcelFile('Excel.xlsx')
#Delete all worksheet that begin with the import_identifier
worksheets = []
for x in all_worksheets:
if x[0] != import_identifier:
worksheets.append(x)
#Loop through the sheets which are flagged for importing and import each
#sheet individually into a dataframe
for sheetname in worksheets:
#Encase the sheetname in quotation marks to satisfy the sheetname function in read_excel
sheetname_macro_str = '"{}"'.format(sheetname_macro)
#Import the workbook and save to dynamically named dataframe
sheetname_macro = pd.read_excel(xls, sheetname=sheetname_macro_str)
#What would I return here, how do I ensure the data frames are stored?
#return