热线电话:13121318867

登录
2019-04-11 阅读量: 1200
Python按多列对DataFrame排序

定义数据框DataFrame

import  pandas

frame = pandas.DataFrame({"a":[9,2,5,1,0,7],"b":[4,7,-3,2,2,2],"c":[6,5,8,3,4,4]})

frame

Out[73]: 

   a  b  c

0  9  4  6

1  2  7  5

2  5 -3  8

3  1  2  3

4  0  2  4

5  7  2  4

升序

 
frame.sort(columns = ['b','c','a'],axis = 0,ascending = True)

Out[74]: 

   a  b  c

2  5 -3  8

3  1  2  3

4  0  2  4

5  7  2  4

0  9  4  6

1  2  7  5

frame.sort_index(axis = 0,ascending = True,by = ['b','c','a'])

Out[75]: 

   a  b  c

2  5 -3  8

3  1  2  3

4  0  2  4

5  7  2  4

0  9  4  6

1  2  7  5

frame.sort_values(by = ['b','c','a'],axis = 0,ascending = True)

Out[76]: 

   a  b  c

2  5 -3  8

3  1  2  3

4  0  2  4

5  7  2  4

0  9  4  6

1  2  7  5

降序

frame.sort(columns = ['b','c','a'],axis = 0,ascending = False)

Out[77]: 

   a  b  c

1  2  7  5

0  9  4  6

5  7  2  4

4  0  2  4

3  1  2  3

2  5 -3  8

frame.sort_index(axis = 0,ascending = False,by = ['b','c','a'])

Out[78]: 

   a  b  c

1  2  7  5

0  9  4  6

5  7  2  4

4  0  2  4

3  1  2  3

2  5 -3  8

frame.sort_values(by = ['b','c','a'],axis = 0,ascending = False)

Out[79]: 

   a  b  c

1  2  7  5

0  9  4  6

5  7  2  4

4  0  2  4

3  1  2  3

2  5 -3  8


0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子