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
user9588528
  • 361
  • 1
  • 4
  • 13

5 Answers5

37

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.

Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37
  • 5
    This worked, and I switched to the `del df.index.name` which I realise now gave me a more correct solution. – user9588528 Oct 28 '18 at 23:33
  • does this work on multipindex? I get 'AttributeError: name' – user-2147482637 Aug 02 '19 at 06: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)` – Peter Leimbigler Aug 03 '19 at 14:19
  • 2
    This seems to have broken in Pandas 1.0.3, [this is now the best answer](https://stackoverflow.com/a/61312610/2098573). – rysqui Jul 01 '20 at 20:40
  • If you do this still "none" or "NAN" will appear while you visualize your dataframe let's say for example in a jupyter notebook. I've found out from [this solution](https://stackoverflow.com/a/57738153/5127304) that actually the best way is to put `df.columns.name=''` – Elias Jan 22 '21 at 07:53
  • 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 '21 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

Rocky Li
  • 5,641
  • 2
  • 17
  • 33
  • 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 '22 at 01: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'

Ian Logie
  • 389
  • 3
  • 7
4

In Pandas 1.0.3 I have used the following :

df.rename_axis(None, axis = 1)
DBear
  • 91
  • 1
  • 1
2

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

df.index.name = None
Michael Li
  • 647
  • 2
  • 8
  • 20