Home > other >  Reading a specific sheet tab using Pandas Python off a XLS file
Reading a specific sheet tab using Pandas Python off a XLS file

Time:09-26

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)

enter image description here

CodePudding user response:

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.

CodePudding user response:

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')
  • Related