python sort、sort_index方法代码实例

yipeiwu_com7年前Python基础

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

对Series进行排序

#生成序列obj
obj=pd.Series([4,9,6,20,4],index=['d','a','e','b','c'])
d   4
a   9
e   6
b  20
c   4
dtype: int64

#按obj的索引排序,默认升序,降序可在括号加ascending=False
obj.sort_index()
a   9
b  20
c   4
d   4
e   6
dtype: int64

#按obj的值排序,默认升序
obj.order()
d   4
c   4
e   6
a   9
b  20
dtype: int64

对DataFrame进行排序

#生成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
C  3  5  2  6
A  9  23 12 34
B  12 15 11 0

#按frame的行索引进行排序
frame.sort_index()
  c  f  d  a
A  9  23 12 34
B  12 15 11 0
C  3  5  2  6

#按frame的列索引进行排序
frame.sort_index(axis=1)
  a  c  d  f
C  6  3  2  5
A  34 9  12 23
B  0  12 11 15

#按frame的一个列或多个列的值进行排序
frame.sort_index(by='a')
  c  f  d  a
B  12 15 11 0
C  3  5  2  6
A  9  23 12 34
frame.sort_index(by=['a','c'])
  c  f  d  a
B  12 15 11 0
C  3  5  2  6
A  9  23 12 34

以上所述是小编给大家介绍的python sort、sort_index方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python自动安装pip

如果是windows安装完成后,需要将'\Python27\Scripts\'加入系统环境变量复制代码 代码如下:# coding=utf-8import osimport urllib...

python 布尔操作实现代码

和别的语言布尔类型定义1为真,0为假不同,python定义的真假比较多。 先说下假吧: false,none,0,"",{},[],() 而真的话,只要和上面的相反就行,比如上面是fal...

Django如何防止定时任务并发浅析

前言 django提供了commands类,允许我们编写命令行脚本,并且可以通过python manage.py拉起。 了解commands 具体django commands如何使用...

Python 调用PIL库失败的解决方法

今天学习Python的时候,需要安装一个第三方库,Python Imaging Library,是Python下面一个非常强大的处理图像的工具库,不过PIL目前只支持到Python2.7...

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

Python使用pip安装报错:is not a supported wheel on this platform的解决方法

本文讲述了Python使用pip安装报错:is not a supported wheel on this platform的解决方法。分享给大家供大家参考,具体如下: 可能的原因1:安...