Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - remove the label of the column index

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
like image 448
user9588528 Avatar asked Oct 28 '18 23:10

user9588528


Video Answer


3 Answers

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.

like image 131
Peter Leimbigler Avatar answered Oct 16 '22 08:10

Peter Leimbigler


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

like image 16
Rocky Li Avatar answered Oct 16 '22 08:10

Rocky Li


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'

like image 14
Ian Logie Avatar answered Oct 16 '22 08:10

Ian Logie