我有一个2000行和500列的数据帧。我想按升序对每列进行排序。这些列没有名称,只是编号为0-500。
随机数据:
df = pandas.DataFrame(np.random.randint(0,100,size=(2000, 500)), columns=range(500))
使用
df.sort_values(by=0,axis=0)
按预期排序第0列。但是然后使用df.sort_values(by=1,axis=0)
对第1列进行排序,但是再次对第0列进行排序。换句话说,我想要
index 0 1 2
1 5 5 5
2 6 7 5
3 7 9 8
但我一次只能排序一列。我已经尝试了df.sort_values(by=df.columns[0:524],axis=0)
,但这会引发一个关键错误。
答案 0 :(得分:7)
我认为您可以将numpy.sort
与DataFrame
构造函数一起使用,或apply
与sort_values
一起使用,numpy array
转换为values
:
df = pd.DataFrame(np.sort(df.values, axis=0), index=df.index, columns=df.columns)
另一种解决方案:
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
答案 1 :(得分:2)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<style>
.table-responsive{
overflow: auto;
max-height: 100px;
}
</style>
</head>
<body>
<div class="header">Head</div>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer egestas elit a ante varius lacinia. Vivamus posuere pellentesque pulvinar. Aliquam erat volutpat. Etiam imperdiet tortor at augue dignissim vehicula. Praesent sit amet nulla efficitur,
viverra arcu non, laoreet mauris. Maecenas eleifend tempus ligula accumsan venenatis. Morbi fermentum lectus at dictum commodo. Nullam commodo in nibh at dapibus. Maecenas sit amet nulla eget massa ultricies porttitor. Aenean blandit lacus a pellentesque
blandit. Phasellus sit amet nisl id risus lobortis imperdiet convallis a augue. Proin nec lectus vel dui ultrices lacinia. In pretium justo nunc, at sodales augue dictum nec. Morbi quis libero molestie odio viverra porta. Integer varius elit sed
tempor mollis. Curabitur finibus volutpat dictum. </p>
<div style="height:150px;background-color:yelow">
<h2>This needs to be fixed and below table region should be scrollable</h2>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>
[HEADER]
</th>
<th>
[HEADER]
</th>
<th>
[HEADER]
</th>
<th>
[HEADER]
</th>
<th>
[HEADER]
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
<tr>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
<td>
[CONTENT]
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
答案 2 :(得分:0)
df.sort(['col1','col2', ..., 'colN'],ascending=False)
或
df.sort(list(df.columns),ascending=False)
答案 3 :(得分:0)
我认为当今最优雅的解决方案是df.transform(np.sort)
。
答案 4 :(得分:0)
要对数据框的所有列进行排序,以替代by=list(df.columns)
,可以使用by=df.columns.to_list()
,如下所示:
df.sort_values(by=df.columns.to_list())
文档: