2017-07-03 214 views
0

是否可以重新排序pandas.DataFrame行(基於索引)inplace? 我期待這樣的事情:將DataFrame行重新排序

df.reorder(idx, axis=0, inplace=True) 

哪裏idx不等於而是同類型的df.index,它包含相同的元素,但在另一份訂單。輸出爲df,在新的idx之前重新排序。

我還沒有找到任何文件,我沒有使用reindex_axis。這讓我希望這是可能的,因爲:

一個新的對象產生,除非新的索引相當於 當前和複製=假

我可能誤會意味着什麼等價指數這個背景。

+0

有你'df.reindex'試過嗎? https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html –

+0

@AdrienMatissart,但它沒有就地開關。 – jlandercy

回答

1

嘗試使用reindex功能(請注意,這不是就地):

>>import pandas as pd 

>>df = pd.DataFrame({'col1':[1,2,3],'col2':['test','hi','hello']}) 
>>df 
    col1 col2 
0 1 test 
1 2 hi 
2 3 hello 


>>df = df.reindex([2,0,1]) 
>>df 
    col1 col2 
2 3 hello 
0 1 test 
1 2 hi 
+0

我知道這一點,但我正在尋找inplace reindex – jlandercy

+0

好的 - 對不起,我不知道這種方法。 – qbzenker