Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Always selecting the first sheet/tab in an Excel Sheet

I know how to get the list of sheet names. The excel file I am using has multiple sheets. How do I select the first one sequentially ? I don't know the name of the sheet but I need to select the first one. How would I go about this ?

like image 218
Meghdeep Ray Avatar asked May 27 '15 09:05

Meghdeep Ray


People also ask

How do I get pandas to read first sheet in Excel?

Use pandas. read_excel() function to read excel sheet into pandas DataFrame, by default it loads the first sheet from the excel file and parses the first row as a DataFrame column name. Excel file has an extension . xlsx.

How do I view different Excel sheets in pandas?

sheet_name param on pandas. read_excel() is used to read multiple sheets from excel. This supports reading excel sheets by name or position. When you read multiple sheets, it creates a Dict of DataFrame, each key in Dictionary is represented as Sheet name and DF for Dict value.

How do you select a specific sheet in Excel in Python?

Log. Message("Row: " + VarToString(i), s); Excel.

How do I select a specific sheet in pandas?

Answer. You need adding the sheet_name parameter to the generated pandas' read_excel() function code to access specific sheet from Excel file that contains multiple sheets.


2 Answers

The first sheet is automatically selected when the Excel table is read into a dataframe.

To be explicit however, the command is :

import pandas as pd
fd = 'file path'
data = pd.read_excel( fd, sheet_name=0 )

Use of 'sheetname' is deprecated. Please use sheet_name

like image 118
Meghdeep Ray Avatar answered Sep 26 '22 01:09

Meghdeep Ray


Also this bug at the time of writing: https://github.com/pandas-dev/pandas/issues/17107

Use 'sheetname', not 'sheet_name'.

like image 29
RGH Avatar answered Sep 26 '22 01:09

RGH