简体   繁体   中英

Reading a specific sheet tab using Pandas Python off a XLS file

I need to read the second tab(sheet two) only of the XLS file I have. I am able to read the file but it always gives the default 1st tab(sheet one). Below are the codes I wrote.

my_excel = pd.read_excel(r'c:\folder\file_name.xls , sheet_name = 'sheet two')

or

my_excel = pd.read_excel("c:\\folder\\file_name.xls" , sheet_name = 1)

I tried both ways but only shows data from Tab one(Sheet one = 0) *Please note the type of the file is Microsoft excel 97-2003 worksheet(xls)

在此处输入图像描述

try this

xls = pd.ExcelFile('path_to_file.xls')
df = pd.read_excel(xls, sheet_name = 'Your sheet name')

or

#df = pd.read_excel(xls, sheet_name = None)
df = pd.read_excel('path_to_file.xls', sheet_name = None) # pandas will read all the sheets from an excel workbook by setting 

sheet_name = None


df["sheet name"] #will return a DataFrame from the sheet you specified.

First, make sure to use the latest version of pandas.
Rn this from your terminal:

pip install --upgrade pandas

Then, use pandas.read_excel to read a specific worksheet.

With either the worksheet's index:

my_excel = pd.read_excel(r"c:\folder\file_name.xls", sheet_name=1)

Or the worksheet's name:

my_excel = pd.read_excel(r"c:\folder\file_name.xls", sheet_name='Swap Rate')

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