Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove index name in pandas

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
like image 403
markov zain Avatar asked Apr 21 '15 07:04

markov zain


People also ask

How do I get rid of the index in pandas?

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 the index and header from a Dataframe?

Just simply put header=False and for eliminating the index using index=False.

How do I drop an index name?

To drop a non-primary key index, use the DROP INDEX command: DROP INDEX index_name ON table_name; The syntax requires the table name to be specified because MySQL allows index names to be reused on multiple tables.

How do I remove a label from a pandas Dataframe?

Pandas DataFrame: drop() functionWhen using a multi-index, labels on different levels can be removed by specifying the level. Index or column labels to drop. Whether to drop labels from the index (0 or 'index') or columns (1 or 'columns'). Alternative to specifying axis (labels, axis=0 is equivalent to index=labels).


6 Answers

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
like image 112
EdChum Avatar answered Oct 02 '22 15:10

EdChum


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 :(

like image 34
Matthew Withrow Avatar answered Oct 02 '22 15:10

Matthew Withrow


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
like image 36
S Anand Avatar answered Oct 02 '22 16:10

S Anand


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
like image 35
jezrael Avatar answered Oct 02 '22 15:10

jezrael


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

df = df.rename_axis(index=None)
like image 39
Kang Avatar answered Oct 02 '22 14:10

Kang


Simple change -- do it inplace:

df_degree.rename_axis(None, axis=1, inplace=True)
like image 33
Matt Avatar answered Oct 02 '22 16:10

Matt