Boo*_*d16 2 python sorting dataframe pandas
我有 df 看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
我想按升序排序,但我不希望排序中包含“A gauche”和“A droite”。
下面的代码做了我想要的,但我不确定如何从排序中排除“A gauche”和“A droite”。
df_table = df_table.sort(columns="Base",ascending=True)
Run Code Online (Sandbox Code Playgroud)
预期产出
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
Run Code Online (Sandbox Code Playgroud)
谢谢
您可能想过滤掉不希望包含在排序操作中的行:
d = df_table
condition = (d.Label=='A gauche') | (d.Label=='A droite')
excluded = d[condition]
included = d[~condition]
Run Code Online (Sandbox Code Playgroud)
然后可以排序
sorted = included.sort(columns="Base",ascending=True)
Run Code Online (Sandbox Code Playgroud)
如果您希望将排除的行附加到数据框的末尾,您可以这样做:
pandas.concat([sorted,excluded])
Run Code Online (Sandbox Code Playgroud)