Add/Remove Index Column by Pandas read_excel

What is Index Column using Pandas to Read Excel Files

First, let us show you our sample Excel file (Workbook) loaded in Python by the Pandas read_excel method.

Pandas index col excel

Code

import pandas as pd

#Loading excel file without index column

dt_frame = pd.read_excel('test_Excel.xlsx',index_col=None)


#Display first sheet

print(dt_frame)

Output:

Pandas index col default image

In the above output, you can see a column is added by Pandas as we are reading a Workbook sheet.

This column containing row numbers is called index column.

In certain scenarios, it may be unnecessary to have this column in our output.

So how to hide/remove this from the returned results?

Excluding default Index column example

The read_excel method has a parameter index_col whose default value is:

  • index_col=None
  • Other values supported are int, and list of int.
  • It is used as the row- label of the Data Frames.
  • Its index starts at 0

While using None (default), it returns the rows as shown in the above graphic – with an index column.

By using index_col = 0, the first column from the Excel sheet is taken as the index column.

See an example below where we used it:

import pandas as pd

#Loading excel file without index column = 0

df_prods = pd.read_excel('test_Excel.xlsx',index_col=0)


#Display first sheet

print(df_prods)

Output:

Pandas index col-0

What if we use 1 or another value

See the example below where we assigned index_col=1:

import pandas as pd

#Using index_col = 1

df_prods = pd.read_excel('test_Excel.xlsx',index_col=1)

#Display first sheet

print(df_prods)

Result:

Pandas index col-1

As index_col is 0-based index, so 1 means the second column from our Excel sheet – is taken as the Index column.
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!