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.

Fetching from specified row in an Excel sheet by Pandas library

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

While we have a tutorial for loading Workbooks and specific sheets in 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

Well, 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-sample

You can see, 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

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-heade

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

You may also want to learn related to this topic:

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