美文网首页
Pandas的基本功能(六)

Pandas的基本功能(六)

作者: 5f2a6061653d | 来源:发表于2018-12-03 20:26 被阅读3次

    排序与排名

    根据实际需求对数据集(Series、DataFrame)进行排序是一种重要的运算,Pandas中的sort_index()函数可根据行索引或列索引按照字典顺序进行排序或排名,可分为对Series对象排序和对DataFrame对象排序。

    对Series对象排序

    对Series对象排序可分为按索引排序和按值排序两种,接下来详细讲解两种排序的实现。

    按索引排序

    具体示例如下所示:

    In [55]: import numpy as np
    In [56]: from pandas import Series
    In [57]: se = Series([1,2,3,4,5],index = ['b','a','d','e','c'])
    In [58]: se1 = se.sort_index()
    In [59]: se2 = se.sort_index(ascending = False)
    

    上述代码是对Series对象按索引进行排序,其中In [58]是对Series对象按索引排序,使用默认排序方式(升序),并将排序后的结果赋值给变量se1;In [59]是对Series对象按索引进行降序排序,并将排序后的结果赋值给变量se2。各变量的运行结果如下所示:

    In [60]: se
    Out[60]:
    b 1
    a 2
    d 3
    e 4
    c 5
    dtype: int64
    In [61]: se1
    Out[61]:
    a 2
    b 1
    c 5
    d 3
    e 4
    dtype: int64
    In [62]: se2
    Out[62]:
    e 4
    d 3
    c 5
    b 1
    a 2
    dtype: int64
    

    从上述运行结果可看出,使用sort_index()函数可对Series对象进行索引排序,并可指定排序方式,默认排序方式为升序排序,将sort_index()函数的ascending参数设置为False,则将排序方式设置为降序排序。

    按值排序

    具体示例如下所示:

    In [63]: import numpy as np
    In [64]: from pandas import Series
    In [65]: se = Series([6,4,7,9,np.nan],index = ['b','a','d','e','c'])
    In [66]: se1 = se.sort_values()
    In [67]: se2 = se.sort_values(ascending = False)
    

    上述代码是对Series对象按值进行排序,其中In [66]是对Series对象按值排序,使用默认排序方式(升序),并将排序后的结果赋值给变量se1;In [67]是对Series对象按索引进行降序排序,并将排序后的结果赋值给变量se2。各变量的运行结果如下所示:

    In [68]: se
    Out[68]:
    b 6.0
    a 4.0
    d    7.0
    e 9.0
    c NaN
    dtype: float64
    In [69]: se1
    Out[69]:
    a 4.0
    b 6.0
    d 7.0
    e 9.0
    c NaN
    dtype: float64
    In [70]: se2
    Out[70]:
    e 9.0
    d 7.0
    b 6.0
    a 4.0
    c NaN
    dtype: float64
    

    从上述运行结果可看出,使用sort_values()函数可对Series对象进行值排序,并可指定排序方式,默认排序方式为升序排序,将sort_values()函数的ascending参数设置为False,则将排序方式设置为了降序排序。

    注意:对值进行排序时,无论是升序排序还是降序排序,缺失值(NaN)都将排在最后面。

    对DataFrame对象排序

    同样地,对DataFrame对象排序也可分为按索引排序和按值排序两种,接下来详细讲解两种排序的实现。

    按索引排序

    具体示例如下所示:

    In [1]: import numpy as np
    In [2]: from pandas import Series,DataFrame
    In [3]: f = DataFrame(np.arange(9).reshape(3,3),index = ['3','1','2'],
     ...: columns = ['b','c','a'])
    In [4]: f1 = f.sort_index()
    In [5]: f2 = f.sort_index(ascending = False)
    In [6]: f3 = f.sort_index(axis = 1)
    In [7]: f4 = f.sort_index(ascending = False,axis = 1)
    

    上述代码是对DataFrame对象按索引进行排序,其中In [4]是对DataFrame对象按行索引排序,使用默认排序方式(升序),并将排序后的结果赋值给变量f1;In [5]是对DataFrame对象按行索引进行降序排序,并将排序后的结果赋值给变量f2;In [6]是对DataFrame对象按列索引排序,使用默认排序方式(升序),并将排序后的结果赋值给变量f3;In [7]是对DataFrame对象按列索引进行降序排序,并将排序后的结果赋值给变量f4。各变量的运行结果如下所示:

    In [8]: f
    Out[8]:
     b c a
    3 0 1 2
    1 3 4 5
    2 6 7 8
    In [9]: f1
    Out[9]:
     b c a
    1 3 4 5
    2 6 7 8
    3 0 1 2
    In [10]: f2
    Out[10]:
     b c a
    3 0 1 2
    2 6 7 8
    1 3 4 5
    In [11]: f3
    Out[11]:
     a b c
    3 2 0 1
    1 5 3 4
    2 8 6 7
    In [12]: f4
    Out[12]:
     c b a
    3 1 0 2
    1 4 3 5
    2 7 6 8
    

    从上述运行结果可看出,使用sort_index()函数可对DataFrame对象进行行或列索引排序,还可指定排序方式,默认排序方式为升序排序,将sort_index()函数的ascending参数设置为False,则将排序方式设置为了降序排序。

    按值排序

    具体示例如下所示:

    In [13]: from pandas import DataFrame
    In [14]: f = DataFrame([[5,2,1],[6,9,2],[5,3,6]],index = ['3','1','2'],
     ...: columns = ['b','a','c'])
    In [15]: f1 = f.sort_values(by = 'a')
    In [16]: f2 = f.sort_values(by = ['b','a'])
    In [17]: f3 = f.sort_values(by = '3',axis = 1)
    In [18]: f4 = f.sort_values(by = 'a',ascending = False)
    

    上述代码是对DataFrame对象按值进行排序,其中In [15]是对DataFrame对象根据列索引为“a”的值排序,即根据一个列的值排序,使用默认排序方式(升序),并将排序后的结果赋值给变量f1;In [16]是对DataFrame对象根据列索引为“b”和“a”的值排序,即根据多个列的值排序,使用默认排序方式(升序),并将排序后的结果赋值给变量f2;In [17]是按指定行值进行排序,同样使用默认排序方式(升序),并将排序后的结果赋值给变量f3;In [18]是对In [15]进行降序排序,并将排序后的结果赋值给变量f4。各变量的运行结果如下所示:

    In [19]: f
    Out[19]:
     b a c
    3 5 2 1
    1 6 9 2
    2 5 3 6
    In [20]: f1
    Out[20]:
     b a c
    3 5 2 1
    2 5 3 6
    1 6 9 2
    In [21]: f2
    Out[21]:
     b a c
    3 5 2 1
    2 5 3 6
    1 6 9 2
    In [22]: f3
    Out[22]:
     c a b
    3 1 2 5
    1 2 9 6
    2 6 3 5
    In [23]: f4
    Out[23]:
     b a c
    1 6 9 2
    2 5 3 6
    3 5 2 1
    

    从上述运行结果可看出,使用sort_values()函数可对DataFrame对象进行值排序,并可指定排序方式,默认排序方式为升序排序,将sort_values()函数的ascending参数设置为False,则将排序方式设置为了降序排序。

    注意:对DataFrame对象进行排序时,必须使用by参数指定某行(列)或某几行(列),否则会报错;在对指定行值进行排序时,必须将axis参数设置为1,否则会报错。

    排名(ranking)与排序关系密切,它会增设一个排名值(从1开始,一直到数组中的有效数据的数量),排名与numpy.argsort产生的间接排序索引类似,只是它还可以通过某种规则破坏平级关系。Series和DataFrame对象主要是通过rank()函数实现排名,默认情况下,rank()函数通过“为各组分配平均排名”来破坏平级关系。接下来详细介绍Series和DataFrame对象的rank()函数。

    1. Series对象的rank()函数
    具体示例如下所示:

    In [8]: from pandas import Series,DataFrame
    In [9]: se = Series([2,4,1,5,2],index = ['b','c','a','e','d'])
    In [10]: se1 = se.rank()
    In [11]: se2 = se.rank(method = 'first')
    In [12]: se3 = se.rank(ascending = False,method = 'first')
    

    上述代码是使用rank()函数对Series对象进行排名,其中In [10]是对Series对象进行排名,使用默认排名方式(升序),并将排名后的结果赋值给变量se1;In [11]是按照按值在原始数据中的出现顺序分配排名,同样使用默认排名方式(升序),并将排名后的结果赋值给变量se2;In [12]是将In[11]按降序排名,并将排名后的结果赋值给变量se3。各变量的运行结果如下所示:

    In [13]: se
    Out[13]:
    b 2
    c 4
    a 1
    e 5
    d 2
    dtype: int64
    In [14]: se1
    Out[14]:
    b 2.5
    c 4.0
    a 1.0
    e 5.0
    d 2.5
    dtype: float64
    In [15]: se2
    Out[15]:
    b 2.0
    c 4.0
    a 1.0
    e 5.0
    d 3.0
    dtype: float64
    In [16]: se3
    Out[16]:
    b 3.0
    c 2.0
    a 5.0
    e 1.0
    d 4.0
    dtype: float64
    

    从上述运行结果可看出,se1中索引“b”和“d”内容都是“2”,由于排名是根据值的大小进行平均排名,两个“2”的排名分别是2和3,因此,平均排名为2.5;se2是根据值在数组中出现的顺序进行排名,因此,两个“2”的排名分别是2.0和3.0;se3是对se2的降序排名,因此,两个“2”的排名分别是3.0和4.0。

    上述示例的rank()函数使用了method选项“first”,method选项可以指定rank()函数排名的方式,主要是用于破坏平级关系,具体如表4.7所示。

    method 说明
    average 默认:在相等分组中,为各值分配平均排名
    min 使用整个分组的最小排名
    max 使用整个分组的最大排名
    first 按值在原始数据中的出现顺序分配排名

    2.DataFrame对象的rank()函数
    具体示例如下所示:

    In [35]: from pandas import Series,DataFrame
    In [36]: f = DataFrame([[6,2,3],[2,5,8],[7,1,2]],index = ['3','1','2'],
     ...: columns = ['c','a','b'])
    In [37]: f1 = f.rank()
    In [38]: f2 = f.rank(axis = 1)
    

    上述代码是使用rank()函数对DataFrame对象进行排名,其中In [37]是对DataFrame对象按列进行排名,使用默认排名方式(升序),并将排名后的结果赋值给变量f1;In [38]是对DataFrame对象按行进行排名,使用默认排名方式(升序),并将排名后的结果赋值给变量f2。各变量的运行结果如下所示:

    In [39]: f
    Out[39]:
     c a b
    3 6 2 3
    1 2 5 8
    2 7 1 2
    In [40]: f1
    Out[40]:
     c a b
    3 2.0 2.0 2.0
    1 1.0 3.0 3.0
    2 3.0 1.0 1.0
    In [41]: f2
    Out[41]:
     c a b
    3 3.0 1.0 2.0
    1 1.0 2.0 3.0
    2 3.0 1.0 2.0
    

    从上述运行结果可看出,DataFrame对象的rank()函数可按照行或列进行排名。DataFrame对象的method选项和ascending属性的设置与Series对象相同。

    相关文章

      网友评论

          本文标题:Pandas的基本功能(六)

          本文链接:https://www.haomeiwen.com/subject/zcsbgftx.html