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