【问题标题】:Reset index after sorting data frame排序数据框后重置索引
【发布时间】:2020-04-03 05:29:32
【问题描述】:

获取此数据集。

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

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

然后我重置索引。

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

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

【问题讨论】:

  • df_genes.sort_values(['Cancer_type'], ascending=True).reset_index(drop=True,inplace=True) 是您需要做的。或者更确切地说保存您的排序 df 然后重置索引
  • 成功了,谢谢!

标签: python-3.x pandas dataframe


【解决方案1】:

这个怎么样?

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

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 2021-08-10
    • 2019-12-20
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多