I have a large spreadsheet file (.xlsx) that I'm processing using python pandas. It happens that I need data from two tabs (sheets) in that large file. One of the tabs has a ton of data and the other is just a few square cells.
When I use pd.read_excel()
on any worksheet, it looks to me like the whole file is loaded (not just the worksheet I'm interested in). So when I use the method twice (once for each sheet), I effectively have to suffer the whole workbook being read in twice (even though we're only using the specified sheet).
How do I only load specific sheet(s) with pd.read_excel()
?
Best Answer
Try
pd.ExcelFile
:As noted by @HaPsantran, the entire Excel file is read in during the
ExcelFile()
call (there doesn't appear to be a way around this). This merely saves you from having to read the same file in each time you want to access a new sheet.Note that the
sheet_name
argument topd.read_excel()
can be the name of the sheet (as above), an integer specifying the sheet number (eg 0, 1, etc), a list of sheet names or indices, orNone
. If a list is provided, it returns a dictionary where the keys are the sheet names/indices and the values are the data frames. The default is to simply return the first sheet (ie,sheet_name=0
).If
None
is specified, all sheets are returned, as a{sheet_name:dataframe}
dictionary.