0
当我从DataFrame中删除一些变量时,返回的数据框与我预期的一样,除了index.name被删除。为什么会这样?熊猫:为什么DataFrame.drop(标签)返回一个带有已删除标签名称的数据框
实施例:
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
后 'B' 将被丢弃的数据帧返回(index.name)不再 '第一',但是无。
Q1。是否因为.drop()方法返回一个数据帧,该数据帧有一个 新的索引对象,默认情况下它没有名字?问题二:
Q2。有 无论如何要保持在下降操作index.name作为新 指数仍正确命名 - 它仅仅是 原指数的子集
预计产出将是:
Out[20]:
second d e f
first
a 1 2 3
c 5 6 7
你的意思是你想要放弃''b''行,但是保留''5,6,7'''行的''b''标签? – askewchan 2013-02-27 00:40:23
不,我想删除'b'行,但将整个索引保留在新数据框中,与以前一样。 (即a,c将被命名为'first') – sanguineturtle 2013-02-27 00:44:38
请修改您的问题以包含您的_expected_或_desired_输出。 – askewchan 2013-02-27 00:45:36