脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python sort、sort_index方法代码实例

python sort、sort_index方法代码实例

2021-06-09 00:43我不不不是宅男 Python

这篇文章主要介绍了python sort、sort_index方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文实例为大家分享了python sort、sort_index的具体代码,供大家参考,具体内容如下

对Series进行排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#生成序列obj
obj=pd.Series([4,9,6,20,4],index=['d','a','e','b','c'])
d   4
a   9
e   6
20
c   4
dtype: int64
 
#按obj的索引排序,默认升序,降序可在括号加ascending=False
obj.sort_index()
a   9
20
c   4
d   4
e   6
dtype: int64
 
#按obj的值排序,默认升序
obj.order()
d   4
c   4
e   6
a   9
20
dtype: int64

对DataFrame进行排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#生成frame
frame=pd.DataFrame(pd.Series([3,5,2,6,9,23,12,34,12,15,11,0]).reshape(3,4),columns=['c','f','d','a'],index=['C','A','B'])
  c  f  d  a
3  5  2  6
9  23 12 34
12 15 11 0
 
#按frame的行索引进行排序
frame.sort_index()
  c  f  d  a
9  23 12 34
12 15 11 0
3  5  2  6
 
#按frame的列索引进行排序
frame.sort_index(axis=1)
  a  c  d  f
6  3  2  5
34 9  12 23
0  12 11 15
 
#按frame的一个列或多个列的值进行排序
frame.sort_index(by='a')
  c  f  d  a
12 15 11 0
3  5  2  6
9  23 12 34
frame.sort_index(by=['a','c'])
  c  f  d  a
12 15 11 0
3  5  2  6
9  23 12 34

以上所述是小编给大家介绍的python sort、sort_index方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/xu200yang/article/details/70239109

延伸 · 阅读

精彩推荐