0

How to run pandas for multiple sheet but with exception of one? I have multiple documents with random sheet number and I need to run them all but with exception of one with description so I need to skip that one.

sheets = pd.read_excel('file.xlsx', sheet_name=None, index_col=[0])

Any ideas?

smci
  • 32,567
  • 20
  • 113
  • 146
project.py
  • 107
  • 1
  • 4
  • 19
  • 1
    The sheet you want to exclude is named `description`? – Corralien Aug 01 '21 at 22:18
  • Yes, the sheet I want to exclude is named 'description'. – project.py Aug 01 '21 at 22:25
  • 1
    Answered by [Vikash Singh's answer](https://stackoverflow.com/a/46081870/202229). First, read in all the sheets, get a list `sheetnames` of their names, then drop the unwanted one with (say)`sheetnames.remove('description')`, then reread them with `sheet_name=sheetnames` – smci Aug 01 '21 at 22:32
  • 1
    @Corralien: you mean `names=`. Ok but that would no longer be the OP's question. `read_excel()` has 27 parameters so yes there are lots of ways to break it when reading multiple sheets. But if you have a separate question from the OP, post your own question (or post an answer to [Using Pandas to pd.read_excel() for multiple worksheets of the same workbook](https://stackoverflow.com/questions/26521266/using-pandas-to-pd-read-excel-for-multiple-worksheets-of-the-same-workbook)). – smci Aug 01 '21 at 22:39

1 Answers1

1

Like the doc says, pd.read_excel(..., sheet_name=...) parameter allows you to specify a list of names, or list of indices. So just list all the sheets you want to import.

UPDATE: the parameter is sheet_name, there's no parameter exclude_sheet_name, so yes if you don't know the list of names/indices of the sheets you do want to read, you'll have to pd.read_excel() twice, and yes that can be time-consuming for huge sheets. If you don't like that, file an enhance request on pandas. Also, 'sheet_name'(/'exclude_sheet_name) can currently only be names or indices, not a regex. Either/both of those would be legit enhance requests.

sheet_name str, int, list, or None, default 0

Strings are used for sheet names. Integers are used in zero-indexed sheet positions. Lists of strings/integers are used to request multiple sheets. Specify None to get all sheets.

smci
  • 32,567
  • 20
  • 113
  • 146