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

Question:

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:folderfile_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)

Asked By: Dhakshika

||

Answers:

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.
Answered By: PyPandas

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:folderfile_name.xls", sheet_name=1)

Or the worksheet’s name:

my_excel = pd.read_excel(r"c:folderfile_name.xls", sheet_name='Swap Rate')
Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.