更改pandas数据框中的行顺序,而不会丢失或弄乱数据 [英] Changing row order in pandas dataframe without losing or messing up data

查看:74
本文介绍了更改pandas数据框中的行顺序,而不会丢失或弄乱数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

(Index)    sample    reads yeasts    
9          CO ref    10
10         CO raai   20
11         CO tus    30

我想基于sample预期的输出来更改列的顺序:

I want to change the order of the columns based on sample, expected output:

(Index)    sample    reads yeasts    
9          CO ref    10
11         CO tus    30
10         CO raai   10

我对行的索引不感兴趣.

I'm not interested in the Index of the rows.

我已经尝试过以下基于其他stackoverflow/google帖子的代码:

I've tried following code based on other stackoverflow/google posts:

df=df.reindex(["CO ref","CO tus","CO raai"])

这可以正确地更改索引,但是所有其他列都获得值nan

This correctly changes the index, but all the other columns get value nan

我也尝试过:

df.index=["CO ref","CO tus","CO raai"]  

这会正确更改索引,但其他列不会切换,因此会弄乱数据框.

This changes the index correctly but the other columns do not switch so it messes up the dataframe.

也:

df["sample"].index=["CO ref","CO tus","CO raai"]   

但这什么也没做.

如何使它正常工作?

推荐答案

对于reindex是必要的,请从sample列中创建索引:

For reindex is necessary create index from sample column:

df=df.set_index(['sample']).reindex(["CO ref","CO tus","CO raai"]).reset_index()

或使用有序分类:

cats = ["CO ref","CO tus","CO raai"]
df['sample'] = pd.CategoricalIndex(df['sample'], ordered=True, categories=cats)
df = df.sort_values('sample')

这篇关于更改pandas数据框中的行顺序,而不会丢失或弄乱数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆