Dear DQ Community,
Grateful for this platform and the help you provide.
I am trying to read-in multiple sheets from excel and instead of having a new line of code every time I read in a new sheet, I wanted to automate this process.
excel_file = ‘XYZ_sample_data_analysis.xlsx’
xyz_1996 = 0
list_years = [‘xyz_1996’, ‘xyz_1997’, ‘xyz_1998’, ‘xyz_1999’, ‘xyz_2000’, ‘xyz_2001’, ‘xyz_2002’, ‘xyz_2003’, ‘xyz_2004’, ‘xyz_2005’]
sheet_num = 0
for each in list_years:
each = pd.read_excel(excel_file, sheet_name = sheet_num)
sheet_num += 1
I know that the loop works because I print the results from the sheet within the loop but when I call any of the variables outside of the loop, I get that it is not defined.
‘xyz_1996’ is not defined.
I’m wondering why this is.
I renamed each variable in the list without the single quotes (that is xyz_1996 instead of ‘xyz_1996’) to see if it would make any difference but it did not help
Thank you for your help!