Pandas Python:对数据框进行排序,但不包含给定的行 [英] Pandas Python: sort dataframe but don't include given row
本文介绍了Pandas Python:对数据框进行排序,但不包含给定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的df:
I have df which looks like this:
Label Base
Label
Très à gauche 4.51
Très à droite 10.49
Ni à gauche, ni à droite 24.21
Je ne sais pas 5.60
Au centre 8.69
A gauche 23.74
A droite 22.75
我想按升序对它进行排序,但我不希望在排序中包括"A gauche"和"A droite".
I would like to sort this in acsending order but I dont want "A gauche" and "A droite" included in the sorting.
下面的代码可以满足我的要求,但是我不确定如何从排序中排除"A gauche"和"A droite".
The code below does what I want but I'm not sure how to exclude "A gauche" and "A droite" from the sorting.
df_table = df_table.sort(columns="Base",ascending=True)
预期输出
Label Base
Label
Très à gauche 4.51
Je ne sais pas 5.60
Au centre 8.69
Très à droite 10.49
Ni à gauche, ni à droite 24.21
A gauche 23.74
A droite 22.75
谢谢
推荐答案
您可能希望过滤掉不希望包含在排序操作中的行:
You probably want to filter out the rows you don't want included in your sort operation:
d = df_table
condition = (d.Label=='A gauche') | (d.Label=='A droite')
excluded = d[condition]
included = d[~condition]
然后可以排序
sorted = included.sort(columns="Base",ascending=True)
如果您希望将排除的行追加到数据框的末尾,则可以执行以下操作:
And if you want the excluded rows appended to the end of your data frame, you could do this:
pandas.concat([sorted,excluded])
这篇关于Pandas Python:对数据框进行排序,但不包含给定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文