Python DataFrame 未保存。如何永久分配呢?

标签 python pandas reverse assign reindex

我通过反转其索引将数据帧分配给自身。但是当我再次调用它时,它显示旧的数据框而不反转它。

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/

相关文章:

python - 将一对多映射字典转换为Dataframe

Python 多线程和多处理加速循环

python - 如何在 Python 中的 Google Cloud Function 中运行子进程

python - 未知的字符串格式,python 错误

python - 我在 tf.contrib.learn.LinearClassifier.fit 中传递什么作为 x 和 y 参数

python - 计算可以从字符串组成的唯一字符串的数量

python - 有效地将自定义函数应用于 Pandas 中的组

javascript - 如何反转 suckerfish 菜单的方向(下拉菜单)

dictionary - 在自定义类型中使用迭代器属性

c - K 和 R C 编程练习 1-19