148

I have a dataframe like this one:

In [10]: df
Out[10]: 
         Column 1
foo              
Apples          1
Oranges         2
Puppies         3
Ducks           4

How to remove index name foo from that dataframe? The desired output is like this:

In [10]: df
Out[10]: 
         Column 1             
Apples          1
Oranges         2
Puppies         3
Ducks           4
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
markov zain
  • 11,987
  • 13
  • 35
  • 39

6 Answers6

147

Alternatively you can just assign None to the index.name attribute:

>>> df.index.name = None
>>> print(df)
         Column 1    
Apples          1
Oranges         2
Puppies         3
Ducks           4
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
EdChum
  • 376,765
  • 198
  • 813
  • 562
127

Took me way too long to find an answer that actually worked for me. See below.

df = df.rename_axis(None, axis=1)

I'm sure some of these other answers are working for other people, but they definitely didn't work for me :(

Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
Matthew Withrow
  • 1,453
  • 1
  • 8
  • 8
  • 19
    This is the only answer that worked for me; using rename_axis() and adding axis=1 – zipline86 Nov 03 '20 at 10:49
  • 11
    Confirm that this is the only answer that works for me! – DPatrick Feb 02 '21 at 22:25
  • 1
    Only answer working for me as well. Some answers work with my other DataFrames, but not with this one. I suppose it is related from the formation of the DataFrame from pivot_table() and reset_index(). – TendaiM Jul 24 '22 at 11:50
  • 2
    I too am in the group for which the other solutions didn't work. Because my dataframe is the result of a pivot. This label is not the name of the index. It's the former column title from which the current column names came. – Jim Moser Sep 13 '22 at 12:57
  • This is what worked for me as well, thanks! In addition, one can use 'inplace=True' with this method as well, if desired. – Danilo Jul 31 '23 at 14:41
80

Use del df.index.name

In [16]: df
Out[16]:
         Column 1
foo
Apples          1
Oranges         2
Puppies         3
Ducks           4

In [17]: del df.index.name

In [18]: df
Out[18]:
         Column 1
Apples          1
Oranges         2
Puppies         3
Ducks           4
S Anand
  • 11,364
  • 2
  • 28
  • 23
  • 46
    With `pandas` version 1.0.3, this does not seem to work anymore. It fails with "AttributeError: can't delete attribute". – billjoie May 18 '20 at 04:50
  • 1
    @billjoie do you know how to solve this problem in pandas 1.0.3. , since `del df.index.name` doesn't work – ctrl_z May 20 '20 at 12:54
  • 22
    @mrn, La solution de @EdChum conctionne très bien: `df.index.name = None` – billjoie May 20 '20 at 23:17
  • 2
    @billjoie Bless your heart. Been on this for a little while. That del df.index.name doesn't work with later version of pandas. – Chen Lizi Aug 04 '20 at 20:44
  • Python 3.9.7 and Pandas 1.3.3: AttributeError: can't delete attribute – Say OL Oct 20 '21 at 12:52
54

From version 0.18.0 you can use rename_axis:

print df
         Column 1
foo              
Apples          1
Oranges         2
Puppies         3
Ducks           4

print df.index.name
foo


print df.rename_axis(None)
         Column 1
Apples          1
Oranges         2
Puppies         3
Ducks           4

print df.rename_axis(None).index.name
None

# To modify the DataFrame itself:
df.rename_axis(None, inplace=True)
print df.index.name
None
Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
4

for your case, just use the following code. tested on pandas 1.0.1.

df = df.rename_axis(index=None)
Kang
  • 169
  • 1
  • 5
3

Simple change -- do it inplace:

df_degree.rename_axis(None, axis=1, inplace=True)
GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27
Matt
  • 31
  • 1