Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove just the index name and not the content in Pandas multiindex data frame

Tags:

python

pandas

I have the following tab delimited file (mydata.txt):

Set Coolthing   Route   Organ   Up  Down
set4    foo ID  LN  81  60
set4    bar ID  LN  542 92
set4    foo ID  LV  73  73
set4    bar ID  LV  143 78
set4    foo ID  SP  32  82
set4    bar ID  SP  90  129

And with the following code:

import pandas as pd
df = pd.io.parsers.read_table("http://dpaste.com/3ZTTVQH.txt")
df = df.pivot(index="Coolthing",columns="Organ")
df.drop('Set',axis=1,inplace=True)
df.drop('Route',axis=1,inplace=True)

I have the following data frame:

In [15]: df
Out[15]:
            Up           Down
Organ       LN   LV  SP    LN  LV   SP
Coolthing
bar        542  143  90    92  78  129
foo         81   73  32    60  73   82

Then using df.to_html(index=True, justify="left") create this html:

enter image description here

What I want to do is to remove the index names Organ and Coolthing. Resulting this:

             Up           Down
            LN   LV  SP    LN  LV   SP
bar        542  143  90    92  78  129
foo         81   73  32    60  73   82

How can I achieve that?

like image 506
pdubois Avatar asked May 15 '15 07:05

pdubois


People also ask

How do I remove an index number from a DataFrame?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I remove a label from a pandas DataFrame?

The drop() function is used to drop specified labels from rows or columns. Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names.

How do I remove a name from a data frame?

DataFrame has a method called drop() that removes rows or columns according to specify column(label) names and corresponding axis.


1 Answers

There is a name for single level names and names for multi-level names so for your example you need to do this to clear the names from index and columns:

In [372]:

df.index.name = None
df.columns.names = (None,None)
df
​
Out[372]:
      Up          Down         
      LN   LV  SP   LN  LV   SP
bar  542  143  90   92  78  129
foo   81   73  32   60  73   82
like image 116
EdChum Avatar answered Sep 29 '22 14:09

EdChum