I am trying to read multiple tabs in spreadsheet to different dataframes and once all tabs with data are over the program should stop.
For first part I am looking to
Demo:
file name
In [94]: fn = r'D:\temp\.data\test.xlsx'
creating pandas.io.excel.ExcelFile
object
In [95]: xl = pd.ExcelFile(fn)
it has sheet_names
attribute
In [96]: xl.sheet_names
Out[96]: ['Sheet1', 'aaa']
we can use it for looping through sheets
In [98]: for sh in xl.sheet_names:
...: df = xl.parse(sh)
...: print('Processing: [{}] ...'.format(sh))
...: print(df.head())
...:
Processing: [Sheet1] ...
col1 col2 col3
0 11 12 13
1 21 22 23
2 31 32 33
Processing: [aaa] ...
a b c
0 1 2 3
1 4 5 6
2 7 8 9
a bit more elegant way is to generate a dictionary of DataFrames:
In [100]: dfs = {sh:xl.parse(sh) for sh in xl.sheet_names}
In [101]: dfs.keys()
Out[101]: dict_keys(['Sheet1', 'aaa'])
In [102]: dfs['Sheet1']
Out[102]:
col1 col2 col3
0 11 12 13
1 21 22 23
2 31 32 33
In [103]: dfs['aaa']
Out[103]:
a b c
0 1 2 3
1 4 5 6
2 7 8 9
This will read all sheets and make a dictionary of dataframes:
xl = pd.read_excel('Unique.xlsx', sheet_name=None)
To get specific sheets, you could do:
xl_dict = {}
sheetname_list = ['blah1', 'blah2', 'blah3']
for sheet in sheetname_list:
xl_dict[sheet] = pd.read_excel('Unique.xlsx', sheet_name=sheet)
or:
xl = pd.read_excel('Unique.xlsx', sheet_name=sheetname_list)