0

Getting this exception while using read_excel function in pandas.

I am trying to fetch columns based on the column labels in the header. And getting this error. Can anyone tell me where I am going wrong.

Here's the function:

pd.read_excel(myexcel.xlsx, sheet_name = 'sheet1', names = ['company name', 'service provided'], header = 1)

Getting the following error when I run it:

"Number of passed names did not match number of header fields in the file"

2
  • Header is 0 indexed, pass in header = 0 and drop the names kwarg. Commented Sep 1, 2020 at 8:26
  • The 'names' parameter is used for setting the column names other than that is existing in excel (or setting the column names when excel does not have header row). For this to be successfully, the number of items passed against 'names' should be equal to the number of columns the read_excel reads. This can be corrected either by using 'usecols' parameter which selects only 2 (in your case) columns or by making sure the number of items in the names parameter is equal to the columns with data in the sheet 'sheet1'. Commented Feb 8 at 4:26

1 Answer 1

1
pd.read_excel(myexcel.xlsx, sheet_name = 'sheet1', usecols = ['company name', 'service provided'], header = 1)
Sign up to request clarification or add additional context in comments.

1 Comment

This should solve the problem, see the help at pandas.pydata.org/pandas-docs/stable/reference/api/…

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.