Reputation: 1455
When I drop some variables from a DataFrame, dataframe return as I expect except the when index.name is removed. Why would this be?
Example:
test = pd.DataFrame([[1,2,3],[3,4,5],[5,6,7]], index=['a','b','c'], columns=['d','e','f'])
test
Out[20]:
second d e f
first
a 1 2 3
b 3 4 5
c 5 6 7
#test.index.name = first
#test.columns.name=second
In [27]:
test.drop(['b'])
Out[27]:
second d e f
a 1 2 3
c 5 6 7
After 'b' is dropped the returned dataframe (index.name)
is no longer 'first' but None.
Q1. Is it because the .drop()
method returns a dataframe that has a
new index object which by default would have no name?
Q2. Is there anyway to preserve the index.name
during drop operations as the newindex is still correctly named - it is just a subset of the
original index
Expected Output would be:
Out[20]:
second d e f
first
a 1 2 3
c 5 6 7
Upvotes: 0
Views: 3104
Reputation: 1455
One way to achieve the intended behavior is:
row_name = test.index.name
test = test.drop('b')
test.index.name = row_name
But this isn't ideal.
Upvotes: 1