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 do something like
xls = pd.ExcelFile('Unique.xlsx')
for i in range(1,n): # n should be number of tabs with data
try:
df_Sector(i)=xls.parse('Sheet'+i) # df_Sector(i) has to be dataframe
except:
pass
I want the program to stop once all tabs with data are read
On the Data tab, under Tools, click Consolidate. In the Function box, click the function that you want Excel to use to consolidate the data. In each source sheet, select your data, and then click Add. The file path is entered in All references.
sheet_name param on pandas. read_excel() is used to read multiple sheets from excel. This supports reading excel sheets by name or position. When you read multiple sheets, it creates a Dict of DataFrame, each key in Dictionary is represented as Sheet name and DF for Dict value.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With