简体   繁体   中英

Import all Sheets in a Excel in different dataframes

I have a Excel-File with many sheets. I want to save each sheet in a dataframe. So for example:

df1 = pd.read_excel('Test.XLS', sheet_name = 'sheet1', skiprows = 3)[:-1]
df2 = pd.read_excel('Test.XLS', sheet_name = 'sheet2', skiprows = 3)[:-1]
...
and so on...

But I dont want to do it manually instead I would like to do it in a loop or something like that

You can save the dataframes in a list and loop over the number of sheets. For example, if you have 10 sheets

df_list=[]

for i in range(1,11):
    df_list.append(pd.read_excel('Test.XLS', sheet_name = 'sheet'+str(i), skiprows = 3)[:-1])

In the case that the number of sheets is not known you can use a try-except

df_list=[]
i = 1
while True:
    try:
        df_list.append(pd.read_excel('Test.XLS', sheet_name = 'sheet'+str(i), skiprows = 3)[:-1])
        i+=1
    except:
        break

这将读取所有工作表并将其放入字典(工作簿)中,其中键是工作表名称,值是数据框。

workbook = pd.read_excel('Test.XLS', sheet_name = None, skiprows = 3)

RTFM!

Doc for read_excel says:

sheet_name : str, int, list, or None, default 0
...
None: All sheets.

You can just do:

dfx = pd.read_excel('Test.XLS', sheet_name = None, skiprows = 3)

and you will a dictionary of all the sheets indexed with the sheet name.

This will work for your requirement.

import pandas as pd
xls = pd.ExcelFile('read_sheets.xlsx')
sheets = xls.sheet_names # get the sheets from the excel file
sheet_names = list(sheets)#store the sheets names into list
for i in range(len(sheet_names)):
    globals()['df_'+sheet_names[i]] = pd.read_excel('read_sheets.xlsx', sheet_name = sheets[i])

df_Sheet1 is first dataframe

df_Sheet2 is second dataframe

df_Sheet3 is third dataframe etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM