pandas 对数据框的所有列进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41507040/
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
Sort all columns of a dataframe
提问by OfOurOwn
I have a dataframe of 2000 rows and 500 columns. I want to sort every column in ascending order. The columns don't have names they're just numbered 0-500.
我有一个 2000 行和 500 列的数据框。我想按升序对每一列进行排序。列没有名称,它们只是编号为 0-500。
Random data:
df = pandas.DataFrame(np.random.randint(0,100,size=(2000, 500)), columns=range(500))
随机数据:
df = pandas.DataFrame(np.random.randint(0,100,size=(2000, 500)), columns=range(500))
Using
df.sort_values(by=0,axis=0)
sorts the 0th column, as expected. But then using df.sort_values(by=1,axis=0)
sorts the 1st column but shuffles the 0th column again. In other words, I want
df.sort_values(by=0,axis=0)
正如预期的那样,使用
对第 0 列进行排序。但是然后使用df.sort_values(by=1,axis=0)
对第一列进行排序,但再次对第 0 列进行混洗。换句话说,我想要
index 0 1 2
1 5 5 5
2 6 7 5
3 7 9 8
But I can only ever get one column sorted at a time. I've tried df.sort_values(by=df.columns[0:524],axis=0)
but that throws a key error.
但是我一次只能对一列进行排序。我试过了,df.sort_values(by=df.columns[0:524],axis=0)
但这会引发一个关键错误。
采纳答案by jezrael
I think you can use numpy.sort
with DataFrame
constructor or apply
with sort_values
with convert to numpy array
by values
:
我认为您可以使用numpy.sort
withDataFrame
构造函数或apply
with sort_values
with convert to numpy array
by values
:
df = pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns)
Another solution, slowier:
另一个解决方案,速度较慢:
df = df.apply(lambda x: x.sort_values().values)
print (df)
0 1 2 3 4 5 6 7 8 9 ... 490 491 492 \
0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
1 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
6 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
7 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
8 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
9 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
10 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
11 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
12 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
13 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
14 0 0 0 0 0 0 0 0 0 0 ... 0 0 0
15 0 0 0 0 0 1 0 0 0 0 ... 0 0 0
16 0 0 0 0 0 1 1 0 0 0 ... 0 0 0
17 0 0 0 0 0 1 1 0 0 0 ... 0 0 0
18 0 0 0 0 0 1 1 0 0 0 ... 0 0 0
19 0 0 0 0 0 1 1 1 1 0 ... 0 0 0
20 0 0 1 0 0 1 1 1 1 0 ... 0 0 0
21 0 0 1 0 0 1 1 1 1 1 ... 0 1 0
22 0 1 1 0 0 1 1 1 1 1 ... 0 1 0
23 1 1 1 0 0 1 1 1 1 1 ... 0 1 0
24 1 1 1 0 0 1 1 1 1 1 ... 0 1 0
25 1 1 1 1 0 1 1 1 1 1 ... 0 1 0
26 1 1 1 1 0 1 1 1 1 1 ... 1 1 1
27 1 1 1 1 0 1 1 1 1 1 ... 1 1 1
28 1 1 1 1 0 1 1 1 1 1 ... 1 1 1
29 1 1 1 1 0 1 1 1 1 1 ... 1 1 1
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1970 97 98 98 98 98 98 99 98 98 98 ... 98 98 98
1971 97 98 98 98 98 98 99 98 98 98 ... 98 98 98
1972 98 98 98 98 98 98 99 98 98 98 ... 98 98 98
1973 98 98 98 99 98 98 99 98 98 98 ... 98 98 98
1974 98 98 98 99 98 98 99 98 98 98 ... 98 98 98
1975 98 98 98 99 98 98 99 98 98 98 ... 98 98 98
1976 98 98 98 99 98 98 99 98 99 99 ... 98 98 98
1977 98 98 98 99 98 98 99 98 99 99 ... 98 98 99
1978 98 98 98 99 98 98 99 98 99 99 ... 98 98 99
1979 98 98 98 99 99 99 99 98 99 99 ... 98 98 99
1980 98 98 98 99 99 99 99 98 99 99 ... 98 98 99
1981 99 99 98 99 99 99 99 98 99 99 ... 99 98 99
1982 99 99 98 99 99 99 99 98 99 99 ... 99 98 99
1983 99 99 98 99 99 99 99 98 99 99 ... 99 98 99
1984 99 99 98 99 99 99 99 99 99 99 ... 99 99 99
1985 99 99 98 99 99 99 99 99 99 99 ... 99 99 99
1986 99 99 98 99 99 99 99 99 99 99 ... 99 99 99
1987 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1988 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1989 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1990 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1991 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1992 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1993 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1994 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1995 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1996 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1997 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1998 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
1999 99 99 99 99 99 99 99 99 99 99 ... 99 99 99
493 494 495 496 497 498 499
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0
11 0 0 0 0 0 0 0
12 0 0 0 0 0 0 0
13 0 0 0 0 0 0 0
14 0 0 0 0 0 0 0
15 0 0 0 0 1 0 0
16 0 1 0 0 1 0 0
17 0 1 0 0 1 0 0
18 1 1 0 0 1 0 0
19 1 1 1 0 1 0 0
20 1 1 1 0 1 0 1
21 1 1 1 0 1 0 1
22 1 1 1 0 1 0 1
23 1 1 1 0 1 0 1
24 1 1 1 0 1 0 1
25 1 1 1 0 1 0 1
26 1 1 1 0 1 0 1
27 1 1 1 1 1 0 1
28 1 1 1 1 1 0 1
29 1 1 1 1 1 0 1
... ... ... ... ... ... ... ...
1970 98 98 98 98 98 98 98
1971 98 98 98 98 98 98 98
1972 98 98 98 98 98 98 98
1973 98 98 98 98 98 98 98
1974 98 98 98 99 98 98 98
1975 98 98 98 99 98 98 98
1976 99 98 98 99 98 98 98
1977 99 98 98 99 98 98 98
1978 99 98 98 99 99 98 98
1979 99 99 98 99 99 98 98
1980 99 99 98 99 99 99 99
1981 99 99 98 99 99 99 99
1982 99 99 98 99 99 99 99
1983 99 99 99 99 99 99 99
1984 99 99 99 99 99 99 99
1985 99 99 99 99 99 99 99
1986 99 99 99 99 99 99 99
1987 99 99 99 99 99 99 99
1988 99 99 99 99 99 99 99
1989 99 99 99 99 99 99 99
1990 99 99 99 99 99 99 99
1991 99 99 99 99 99 99 99
1992 99 99 99 99 99 99 99
1993 99 99 99 99 99 99 99
1994 99 99 99 99 99 99 99
1995 99 99 99 99 99 99 99
1996 99 99 99 99 99 99 99
1997 99 99 99 99 99 99 99
1998 99 99 99 99 99 99 99
1999 99 99 99 99 99 99 99
回答by Roman Pekar
>>> df.sort_values(by=list(df.columns),axis=0)
0 1 2
index
1 5 5 5
2 6 7 5
3 7 9 8
回答by TheBamf
I think the most elegant solution nowadays is df.transform(np.sort)
.
我认为当今最优雅的解决方案是df.transform(np.sort)
.
回答by lps
df.sort(['col1','col2', ..., 'colN'],ascending=False)
or
或者
df.sort(list(df.columns),ascending=False)