close

How to remove index from a created Dataframe in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove index from a created Dataframe in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to remove index from a created Dataframe in Python?

  1. How to remove index from a created Dataframe in Python?

    If you want to save your dataframe to a spreadsheet for a report.. it is possible to format the dataframe to eliminate the index column using xlsxwriter.

  2. remove index from a created Dataframe in Python

    If you want to save your dataframe to a spreadsheet for a report.. it is possible to format the dataframe to eliminate the index column using xlsxwriter.

Method 1

You can use set_index, docs:

import pandas as pd

list1 = [1,2]
list2 = [2,5]
df=pd.DataFrame({'Name' : list1,'Probability' : list2})
print (df)
   Name  Probability
0     1            2
1     2            5

df.set_index('Name', inplace=True)
print (df)
      Probability
Name             
1               2
2               5

If you need also remove index name:

df.set_index('Name', inplace=True)
#pandas 0.18.0 and higher
df = df.rename_axis(None)
#pandas bellow 0.18.0
#df.index.name = None
print (df)
   Probability
1            2
2            5

Method 2

If you want to save your dataframe to a spreadsheet for a report.. it is possible to format the dataframe to eliminate the index column using xlsxwriter.

writer = pd.ExcelWriter("Probability" + ".xlsx", engine='xlsxwriter')

df.to_excel(writer, sheet_name='Probability', startrow=3, startcol=0, index=False)

writer.save()

index=False will then save your dataframe without the index column.

I use this all the time when building reports from my dataframes.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read