问题
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
回答1:
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
回答2:
Alternatively you can just assign None
to the index.name
attribute:
In [125]:
df.index.name = None
df
Out[125]:
Column 1
Apples 1
Oranges 2
Puppies 3
Ducks 4
回答3:
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
来源:https://stackoverflow.com/questions/29765548/remove-index-name-in-pandas