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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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
Method 3
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 🙁
Method 4
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
Method 5
for your case, just use the following code. tested on pandas 1.0.1.
df = df.rename_axis(index=None)
Method 6
Simple change — do it inplace:
df_degree.rename_axis(None, axis=1, inplace=True)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0