How to remove index from created Dataframe in Python?

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

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

But I would like to remove the first column (index column) and make the column named Name first column. I tried using del df['index'] and index_col=0 . But they did not work. I also checked reset_index() , and that is not what I need. I would like to completely remove the entire index column from the Dataframe that was created this way (as mentioned above). Someone please help!

+7
python pandas
source share
1 answer

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 also need to remove the name index:

 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 
+13
source share

All Articles