119

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
smci
  • 29,564
  • 18
  • 109
  • 144
markov zain
  • 10,257
  • 13
  • 32
  • 38

6 Answers6

126

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
  • 4,527
  • 4
  • 23
  • 38
EdChum
  • 339,461
  • 188
  • 752
  • 538
78

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
  • 10,326
  • 1
  • 27
  • 21
  • 44
    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
73

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
  • 4,527
  • 4
  • 23
  • 38
Matthew Withrow
  • 883
  • 7
  • 8
51

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
  • 12,769
  • 13
  • 73
  • 119
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
3

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

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

Simple change -- do it inplace:

df_degree.rename_axis(None, axis=1, inplace=True)
GooDeeJAY
  • 1,527
  • 1
  • 17
  • 25
Matt
  • 11
  • 1