32

I have a dataframe as follows:

PLEASE_REMOVE  2013  2014  2015
 THIS_IS_EASY
-------------------------------
          Bob     0     3     4
         Mary     2     3     6

The years (2013, 2014, 2015) are the column index labels. The names (Mary, Bob) are the row index labels.

I have somehow managed to get labels for the row indices and column indices.

Using df.index.names = [''] I can remove the THIS_IS_EASY bit.

How can I remove the PLEASE_REMOVE bit?

Desired output is:

               2013  2014  2015
 -------------------------------
          Bob     0     3     4
         Mary     2     3     6
1
  • What's your data source and sample code? Oct 28, 2018 at 23:21

5 Answers 5

34

New answer for pandas 1.x, submitted by Ian Logie

df.columns.name = None

 

Old Answer from Oct 2018

Simply delete the name of the columns:

del df.columns.name

Also, note that df.index.names = [''] is not quite the same as del df.index.name.

6
  • 5
    This worked, and I switched to the del df.index.name which I realise now gave me a more correct solution. Oct 28, 2018 at 23:33
  • does this work on multipindex? I get 'AttributeError: name' Aug 2, 2019 at 6:40
  • @user-2147482637, use MultiIndex.rename() for that. For example, if df has 2 index levels, you can rename the outermost level (level=0) with: df.index = df.index.rename('new_level_name', level=0) Aug 3, 2019 at 14:19
  • 2
    This seems to have broken in Pandas 1.0.3, this is now the best answer.
    – rysqui
    Jul 1, 2020 at 20:40
  • 1
    In 1.2.4, df.columns.name throws error. It is df.columns.names and it does not take single value, it expects list like df.columns.names = [None, None] if you have multi header. and to remove index label you have to use temp_df.rename_axis(None, inplace=True) seperatly.
    – shiv
    Apr 21, 2021 at 11:02
18

Like this:

df = df.rename_axis(None)

This will get rid of everything on the top left. you can also do it in place: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename_axis.html

1
  • 1
    Not everything. It will only get rid of "THIS IS EASY" in the example in the original post because of the default. df = df.rename_axis(None, axis=1) or df = df.rename_axis(None, axis="columns") to remove the overall name of the columns, i.e. "PLEASE REMOVE" in the example in the original post.
    – Alper
    Aug 21, 2022 at 1:54
16

In Pandas 1.0.3 the following works:

df.columns.name = None

The 'accepted answer' above, del df.columns.name, leads to: 'AttributeError: can't delete attribute'

2
  • 2
    In pandas 1.0.3, the accepted answer does not work anymore. @Ian Logie you are right.
    – Eiffelbear
    May 3, 2020 at 13:28
  • Edited my answer (which was accepted back in October 2018) to reflect this correct way to remove the name of the column index. Thanks @IanLogie! Mar 31, 2021 at 3:56
4

In Pandas 1.0.3 I have used the following :

df.rename_axis(None, axis = 1)
2

columns.name will not work since poster want to remove the index name

df.index.name = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.