Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading multiple tabs from excel in different dataframes

Tags:

python

pandas

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

like image 582
abhi_phoenix Avatar asked Mar 19 '17 14:03

abhi_phoenix


People also ask

How do I pull data from multiple tabs in Excel?

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.

How do I read multiple sheets in Excel using pandas?

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.


2 Answers

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
like image 194
MaxU - stop WAR against UA Avatar answered Nov 14 '22 23:11

MaxU - stop WAR against UA


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)
like image 27
b2002 Avatar answered Nov 14 '22 23:11

b2002