Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to set header and skiprows sparsely in read_excel()

enter image description here

This is my sample external excel file.
I want to set the header(column name) on the 3rd row and read the values starting from the 5th row.

pd.read_excel(file.xlsx, header = [2], skiprows=3)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to using hedaer and skiprows but fail to read correctly.

Is it impossible to do this simultineously when I read it?

>Solution :

You have to wrap the skiprows argument in a list.

from pandas import read_excel

df = read_excel('foo.xlsx', header=[2], skiprows=[3])

print(df)

Results in:

   Unnamed: 0  aapl  msft
0           1     2     3
1           4     5     6

Giving a number n means "skip n rows", giving a list with numbers means "skip these indices".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading