我通过反转其索引将数据帧分配给自身。但是当我再次调用它时,它显示旧的数据框而不反转它。
apple = apple.reindex(index = apple.index[::-1])
apple.head()
如有任何帮助,我们将不胜感激。 预先感谢:)
最佳答案
对我来说效果很好。
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
关于Python DataFrame 未保存。如何永久分配呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45399395/