How to Return Excel Specified Rows by Pandas skiprows

In many situations, you only need to fetch from a specified row rather than loading the whole Excel sheet using Python Pandas.

We also have a tutorial for loading workbooks and specific sheets using the read_excel method.

In this tutorial, we will show you how to skip specific rows or return only rows from the specified number.

Using skiprows attribute in read_excel method

The easiest way is using the skiprows parameter of the read_excel method of Pandas.

In the example below, we will load a sample sheet and only data from row number four to the end will be returned.

For an idea, this is our sample sheet in the “test_Pandas.xlsx” Workbook:

Pandas skiprow tutorial sample data

You can see that it contains a total of eleven rows including the header row.

The code:

import pandas as pd_skip

#Loading a sheet "Products"

df_specified = pd_skip.read_excel('test_pandas.xlsx', sheet_name="Products", skiprows=range(1, 3))


#Return rows after 3 to end

print(df_specified)

Output:

Pandas skiprows output image

You can see in the above graphic and compare the sample sheet – it only started to display from p003 product which is row number 4.

Points to note:

  • In the skiprows parameter, we specified a range from 1,3
  • The row index starts at 0
  • If we have specified 0, 3, the returned result would omit the header row as well. See the example below.

Omitting header row example

In the code below, we specified range(0,3) and see the output:

import pandas as pd_skip

#Loading a sheet "Products" and omitting the header row

df_specified = pd_skip.read_excel('test_pandas.xlsx', sheet_name="Products", skiprows=range(0, 3))


#Return rows after 3 to end

print(df_specified)

Output:

Pandas skiprows header

Skip rows by using a list of row numbers

In the example below, we used a Python list in the skiprows parameter to omit certain rows. We will omit every other row – so display only Odd rows including the header:

import pandas as pd_skip

#Using a list to specify which rows to omit in the data frame

df_specified = pd_skip.read_excel('test_pandas.xlsx', sheet_name="Products", skiprows=[1,3,5,7,9], index_col=0)


#Display data frame

print(df_specified)

Output:

Pandas skiprows even output image

You may also want to learn related to this topic:

Return only Top, and bottom rows by Pandas head and tail methods

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!