Railey Shahril
Railey Shahril

Reputation: 109

Pandas Read Excel Read Second Tab Ignoring the Sheet Name

I want to create a python script to process multiple excel, and I want to take only second sheet on the excel file ignoring the sheet name as the name will not be the same every time.

try1 = pd.ExcelFile('File Path')
try1.sheet_names


Out[41]:
['Analysis Header', 'Total Individuals (4543)']

as you can see the Total individual will change time to time with a random number.

Thank You Best Regards Railey Shahril

Upvotes: 1

Views: 2440

Answers (2)

Harinie R
Harinie R

Reputation: 307

You can use pandas.read_excel option!

pd.read_excel(file_path,sheet_name=1)

"sheet_name" can be sheet_name or sheet_index or list of them mixed up. The code above always take second sheet of workbook as a dataframe which solves your problem no matter what the name of your sheet is!

Take a look in this official pandas.read_excel documentation for more information!

Hope this helps!

Upvotes: 1

Craig
Craig

Reputation: 4855

pd.ExcelFile has a method .parse() which works exactly like pd.read_excel(). Both functions accept the parameter sheet_name which handles many ways of selecting one or more sheets to import. In your case, you want to refer to the sheet number, so you should pass sheet_name an integer value indicating the sheet. Pandas numbers the sheets starting with 0, so the 2nd sheet can be selected with sheet_name=1 as:

pd.ExcelFile('File Path').parse(sheet_name=1)

This is equivalent to:

pd.read_excel('File Path', sheet_name=1)

Th sheet_name and the other parameters for reading Excel files are described in the pandas docs.

Upvotes: 1

Related Questions