python,用熊猫对降序数据框进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24988873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python, sort descending dataframe with pandas
提问by user3636476
I'm trying to sort a dataframe by descending. I put 'False' in the ascending argument, but my order is still ascending.
我正在尝试通过降序对数据框进行排序。我将 'False' 放在升序参数中,但我的顺序仍然是升序。
My code is:
我的代码是:
from pandas import DataFrame
import pandas as pd
d = {'one':[2,3,1,4,5],
'two':[5,4,3,2,1],
'letter':['a','a','b','b','c']}
df = DataFrame(d)
test = df.sort(['one'], ascending=[False])
but the output is
但输出是
letter one two
2 b 1 3
0 a 2 5
1 a 3 4
3 b 4 2
4 c 5 1
采纳答案by U2EF1
Edit: This is out of date, see @Merlin's answer.
编辑:这已过时,请参阅@Merlin 的回答。
[False]
, being a nonempty list, is not the same as False
. You should write:
[False]
,作为一个非空列表,与False
. 你应该写:
test = df.sort('one', ascending=False)
回答by Merlin
New syntax (either):
新语法(或者):
test = df.sort_values(['one'], ascending=[False])
test = df.sort_values(['one'], ascending=[0])
回答by Arko
For pandas 0.17 and above, use this :
对于 0.17 及更高版本的熊猫,请使用:
test = df.sort_values('one', ascending=False)
Since 'one' is a series in the pandas data frame, hence pandas will not accept the arguments in the form of a list.
由于 'one' 是 Pandas 数据框中的一个系列,因此 Pandas 不会接受列表形式的参数。
回答by aspiring1
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html
I don't think you should ever provide the False
value in square brackets (ever), also the column values when they are more than one, then only they are provided as a list! Not like ['one']
.
我认为您不应该False
在方括号中提供值(永远),当列值超过 1 时,也应该提供列值,然后只将它们作为列表提供!不喜欢['one']
。
test = df.sort_values(by='one', ascending = False)
回答by Vivekanand AIML
from pandas import DataFrame
import pandas as pd
d = {'one':[2,3,1,4,5],
'two':[5,4,3,2,1],
'letter':['a','a','b','b','c']}
df = DataFrame(d)
test = df.sort_values(['one'], ascending=False)
test