python-3.x - 排序数据框后重置索引

标签 python-3.x pandas dataframe

获取此数据集。

![Take this dataset.

我按包含 [0,1] 值的 Cancer_type 列对数据框进行了排序,

df_genes.sort_values(['Cancer_type'], ascending=True)

![enter image description here

然后我重置索引。

df_genes.reset_index(drop=True,inplace=True)

![enter image description here

重置索引会弄乱 Cancer_type 列。 我应该如何重置索引以保持 Cancer_type 列排序。 这link遵循相同的程序,但我得到不同的结果。

最佳答案

这个呢?

df_genes = df_genes.sort_values(['Cancer_type']).reset_index(drop=True)

关于python-3.x - 排序数据框后重置索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53332116/

相关文章:

python - 注释 matplotlib 热图

Python:根据其他列中存在的值替换多个列值

python - 圆形 Pandas 数据框/系列

python - 在 Pandas 中格式化 DataFrame - 堆叠列

java - 从嵌套的用户定义对象中创建 DataFrame

python - 将行传递给函数给出错误 Pandas Python

Django 1.11 管理表单将搜索框添加到 oneToOne 字段

python - 删除文件时出错

pandas - 尝试基于多索引合并 pandas 中的 2 个数据帧

r - 按文件扩展名和时间对文件的 data.frame 进行分组