2017-07-30 25 views
1

我通過顛倒索引來爲自己分配數據幀。但是當我再次調用它時,它顯示的是舊數據幀而沒有反轉它。Python DataFrame沒有保存。如何永久分配它?

apple = apple.reindex(index = apple.index[::-1]) 
apple.head() 

任何幫助,將不勝感激。 在此先感謝:)

+1

它的工作原理?你可以發佈整個代碼,也許在別處有問題。 – aristotll

+0

好的。我發現了一些奇怪的東西。當我按下ctrl + Enter在jupyter我得到舊的結果。 ctrl +再次輸入 - >新的結果。 – DICER45

回答

2

對我來說它很好。

np.random.seed(45) 
apple = pd.DataFrame(np.random.randint(10, size=(3,4)), 
        columns=list('abcd'), 
        index=list('ABC')) 

print (apple) 
    a b c d 
A 3 0 5 3 
B 4 9 8 1 
C 5 9 6 8 

apple = apple.reindex(index = apple.index[::-1]) 
print (apple) 
    a b c d 
C 5 9 6 8 
B 4 9 8 1 
A 3 0 5 3 

DataFrame.iloc另一種解決方案:

apple = apple.iloc[::-1] 
print (apple) 
    a b c d 
C 5 9 6 8 
B 4 9 8 1 
A 3 0 5 3 
+0

好的。我發現了一些奇怪的東西。當我按下ctrl + Enter在jupyter我得到舊的結果。 ctrl +再次輸入 - >新的結果... – DICER45

+0

不幸的是,我從來沒有這個問題:( – jezrael

+0

好吧,我剛剛關閉並打開它,現在很好。:D – DICER45