22

I have created a Dataframe df by merging 2 lists using the following command:

import pandas as pd
df=pd.DataFrame({'Name' : list1,'Probability' : list2})

But I'd like to remove the first column (The index column) and make the column called Name the first column. I tried using del df['index'] and index_col=0. But they didn't work. I also checked reset_index() and that is not what I need. I would like to completely remove the whole index column from a Dataframe that has been created like this (As mentioned above). Someone please help!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
controlfreak
  • 3,165
  • 3
  • 12
  • 14

3 Answers3

44

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • There is an insane algorithm that I'm working on. I have used up all my brain cells on it in the last 2 days haha. I'll ask for help if I can't figure it out by tonight. – controlfreak May 20 '16 at 16:40
  • I can try it or maybe someone else. I hope you found solution. Good luck! – jezrael May 20 '16 at 16:41
  • @jezrael This serves the purpose but the id is misplaced in the row. Any fix for that ? – Python Bang Aug 04 '22 at 14:07
8

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
D. Whitt
  • 81
  • 1
  • 3
-1

I think the best way is to hide the index using the hide_index method

df = df.style.hide_index()

this will hide the index from the dataframe.

Somesh Gupta
  • 324
  • 3
  • 13
  • 1
    Did you check this Somesh? On my system this method returns a `pandas.io.formats.style.Styler` object, not a `DataFrame` – user1330734 Apr 23 '21 at 14:21
  • @user1330734 it works, I have tested it with my dataset. can you post your error here? it seems from you error the object you are trying to convert is not a dataframe. – Somesh Gupta Apr 27 '21 at 03:49
  • ```>>> type(df) >>> df2 = df.style.hide_index() >>> type(df2) ``` – user1330734 Apr 28 '21 at 03:57
  • This solution has to install `Jinja2`. That raise this error: `Missing optional dependency 'Jinja2'. DataFrame.style requires jinja2. Use pip or conda to install Jinja2. ` – Mario Nov 17 '21 at 14:34