热线电话:13121318867

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

定义数据框DataFrame

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

frame
Out[53]: 
   a  b  c
0  9  4  6
1  2  7  5
2  5 -3  8
3  1  2  3

升序

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

Out[62]: 
 
 a  b  c

3  1  2  3

1  2  7  5

2  5 -3  8

0  9  4  6


frame.sort_index(axis = 0,ascending = True,by = 'a')

Out[63]: 

   a  b  c

3  1  2  3

1  2  7  5

2  5 -3  8

0  9  4  6


frame.sort_values(by = 'a',axis = 0,ascending = True)

Out[65]: 

   a  b  c

3  1  2  3

1  2  7  5

2  5 -3  8

0  9  4  6

降序


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

Out[67]: 

   a  b  c

0  9  4  6

2  5 -3  8

1  2  7  5

3  1  2  3


frame.sort_index(axis = 0,ascending = False,by = 'a')

Out[68]: 

   a  b  c

0  9  4  6

2  5 -3  8

1  2  7  5

3  1  2  3



frame.sort_values(by = 'a',axis = 0,ascending = False)

Out[69]: 
   
a  b  c

0  9  4  6

2  5 -3  8

1  2  7  5

3  1  2  3

35.5375
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子