0

I have some legacy code, that uses parameter '16' in 'pandas.ExcelFile.parse' function:

xls_file = pd.ExcelFile(xls_file_path) 
df = xls_file.parse('16')

Using the above or just this:

df = xls_file.parse()

returns similar dataframes with all the columns from Excel file. The only difference is that in the last case dataframe has more records then in the first case.

What does "parse('16')" mean? Can not deduce this from pandas docs.

0

4 Answers 4

2

To load a sheet of xls_file by index, pass the sheet index as an int to xls_file.parse(). Remember that the first sheet has index 0 in Python.

Sign up to request clarification or add additional context in comments.

Comments

1

@DeepSpace has already posted a link to the Pandas docs, where we can find the following text:

Equivalent to read_excel(ExcelFile, ...) See the read_excel docstring for more info on accepted parameters

read_excel() docs:

sheetname : string, int, mixed list of strings/ints, 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. str|int -> DataFrame is returned. list|None -> Dict of DataFrames is returned, with keys representing sheets. Available Cases

Defaults to 0 -> 1st sheet as a DataFrame

1 -> 2nd sheet as a DataFrame

“Sheet1” -> 1st sheet as a DataFrame

[0,1,”Sheet5”] -> 1st, 2nd & 5th sheet as a dictionary of DataFrames

None -> All sheets as a dictionary of DataFrames

Comments

1

Judging by the function's signature it is the sheetname.

ExcelFile.parse(sheetname=0, header=0, skiprows=None, skip_footer=0, names=None, 
                index_col=None, parse_cols=None, parse_dates=False, date_parser=None,
                na_values=None, thousands=None, convert_float=True, has_index_names=None,
                converters=None, true_values=None, false_values=None, squeeze=False, **kwds)

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.ExcelFile.parse.html

3 Comments

Looking at this function signature default value for 'sheetname' is 0. So this is a numeric parameter. How can it be a string?
@dokondr Python doesn't care about types. Argument's default value doesn't imply anything
That's fine, yet at some point, 'parse' function needs to know if this is an excel sheet specified by an index number, or by a name in a string. Otherwise 'parse' will have to try both options and find which one works. Is it what you mean?
0

xls_file.sheet_names() will give you the names of the various spreadsheets in the imported excel file. You will then realise that '16' is the name of a spreadsheet in the file. However, if this value was an integer, it will be a representation of the spreadsheet at index 16 in the list returned by xls_file.sheet_names(). Note that indexing starts from 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.