• pandas入门学习--------------------------(一)


    使用pandas,首先需要熟悉它的2个主要的数据结构:Series和DataFrame。

    Series

    series是一种类似于一维数组的的对象,它由一组数据(各种Numpy数据类型)以及一组与之相关的数据标签(索引)组成。

    In [1]: from pandas import Series,DataFrame
    
    In [2]: import pandas as pd
    
    In [3]: obj=Series([4,7,-5,3])
    
    In [4]: obj
    Out[4]:
    0    4
    1    7
    2   -5
    3    3
    dtype: int64

    Series的字符串表现形式为:索引在左边,值在右边。由于没有为数据设定索引,会自动创建一个从0到N-1(N为数据的长度)的整数型索引。可以通过series的values和index属性获取其数组的表现形式和索引对象。

    In [5]: obj.values
    Out[5]: array([ 4,  7, -5,  3], dtype=int64)
    
    In [6]: obj.index
    Out[6]: RangeIndex(start=0, stop=4, step=1)

    另外,还可以创建一个series带有一个对各个数据点进行标记的索引:

    In [7]: obj2=Series([4,7,-5,3],index=['b','c','d','a'])
    
    In [8]: obj2
    Out[8]:
    b    4
    c    7
    d   -5
    a    3
    dtype: int64
    
    In [9]: obj2.index
    Out[9]: Index(['b', 'c', 'd', 'a'], dtype='object')

    与普通的numpy数组相比,你可以通过索引的方式选区series的单个或一组值:

    In [10]: obj2['a']
    Out[10]: 3
    
    In [11]: obj2['d']
    Out[11]: -5
    
    In [12]: obj2['d']=6
    
    In [13]: obj2['d']
    Out[13]: 6
    
    In [14]: obj2[['c','a','d']]
    Out[14]:
    c    7
    a    3
    d    6
    dtype: int64

    注意:选取一组值的时候,obj2[['c','a','d']]是由2个[]组成。

    numpy数组的数组运算都会保留索引与值之间的链接:

    In [15]: obj2
    Out[15]:
    b    4
    c    7
    d    6
    a    3
    dtype: int64
    
    In [16]: obj2[obj2>0]
    Out[16]:
    b    4
    c    7
    d    6
    a    3
    dtype: int64
    
    In [17]: obj2[obj2-5>0]
    Out[17]:
    c    7
    d    6
    dtype: int64
    
    In [18]: obj2*2
    Out[18]:
    b     8
    c    14
    d    12
    a     6
    dtype: int64

    还可以将series看成一个定长的有序字典,因为它是索引值到数据值的一个映射。它可以用到许多需要字典参数的函数中:

    In [23]: 'a'   in obj2
    Out[23]: True
    
    In [24]: 'e'   in obj2
    Out[24]: False

    注意:只能用索引,不能用数据值

    In [26]: obj2
    Out[26]:
    b    4
    c    7
    d    6
    a    3
    dtype: int64
    
    In [27]: 3 in obj2
    Out[27]: False

    如果数据被存放在一个python字典中,也可以直接通过这个字典来创建series:

    In [28]: sdata={'ohio':35000,'Texha':71000,'Oraige':16000,'Utah':5000}
    
    In [29]: obj3=Series(sdata)
    
    In [30]: obj3
    Out[30]:
    ohio      35000
    Texha     71000
    Oraige    16000
    Utah       5000
    dtype: int64

    如果只传入一个字典,则结果中series中的索引就是原字典的键(有序排列)

    In [31]: states=['California','ohio','Utah','Oraige']
    
    In [32]: obj4=Series(sdata,index=states)
    
    In [33]: obj4
    Out[33]:
    California        NaN
    ohio          35000.0
    Utah           5000.0
    Oraige        16000.0
    dtype: float64

    上面中,sdata中跟states索引相匹配的那3个值会被找出来并放到相应的位置上。未找到的就用NaN表示。

    pandas中的isnull和notnull可用于检测缺失数据

    In [34]: pd.isnull(obj4)
    Out[34]:
    California     True
    ohio          False
    Utah          False
    Oraige        False
    dtype: bool
    
    In [35]: pd.notnull(obj4)
    Out[35]:
    California    False
    ohio           True
    Utah           True
    Oraige         True
    dtype: bool

    series中也有类似方法:

    In [37]: obj4.isnull()
    Out[37]:
    California     True
    ohio          False
    Utah          False
    Oraige        False
    dtype: bool
    
    In [38]: obj4.notnull()
    Out[38]:
    California    False
    ohio           True
    Utah           True
    Oraige         True
    dtype: bool

    对于很多应用而言,Series最重要的一个功能就是:它在算术运算中会自动对其不同索引的数据。

    In [39]: obj3
    Out[39]:
    ohio      35000
    Texha     71000
    Oraige    16000
    Utah       5000
    dtype: int64
    
    In [40]: obj4
    Out[40]:
    California        NaN
    ohio          35000.0
    Utah           5000.0
    Oraige        16000.0
    dtype: float64
    
    In [41]: obj3+obj4
    Out[41]:
    California        NaN
    Oraige        32000.0
    Texha             NaN
    Utah          10000.0
    ohio          70000.0
    dtype: float64

    Series对象本身及其索引都有一个name属性,该属性跟pandas其他关键功能关系非常密切:

    In [42]: obj4.name
    
    In [43]: obj4.name='population'
    
    In [44]: obj4
    Out[44]:
    California        NaN
    ohio          35000.0
    Utah           5000.0
    Oraige        16000.0
    Name: population, dtype: float64
    
    In [45]: obj4.index.name='state'
    
    In [46]: obj4
    Out[46]:
    state
    California        NaN
    ohio          35000.0
    Utah           5000.0
    Oraige        16000.0
    Name: population, dtype: float64

    Series的索引可以通过赋值的方式就地修改:

    In [47]: obj
    Out[47]:
    0    4
    1    7
    2   -5
    3    3
    dtype: int64
    
    In [48]: obj.index=['Bob','Steve','Jeff','Ryan']
    
    In [49]: obj
    Out[49]:
    Bob      4
    Steve    7
    Jeff    -5
    Ryan     3
    dtype: int64

    DataFrame

    DataFrame是一个表格型的数据结构。它含有一组有序的列,每列可以是不同的值类型/(数值丶字符串丶布尔值等)。DataFrame既有行索引也有列索引,它可以被看做由Series组成的字典(共同一个索引)。跟其他类似的数据结构相比,DataFrame中面向行和面向列的操作基本上是平衡的。其实,DataFrame中的数据是以一个或多个二维块存放的(而不是列表/字典或别的一维数据结构)。

    构建DataFrame的办法很多,最典型的就是由一个等长列表或Numpy数组组成的字符串:

    In [52]: data={'state':['onio','onio','linux','hp-ux','red-hat'],
        ...: 'year':[2000,2001,2002,2001,2002],
        ...: 'pop':[1.5,1.7,3.6,2.4,2.9]}
    
    In [53]: frame=DataFrame(data)
    
    In [54]: frame
    Out[54]:
         state  year  pop
    0     onio  2000  1.5
    1     onio  2001  1.7
    2    linux  2002  3.6
    3    hp-ux  2001  2.4
    4  red-hat  2002  2.9

    结果DataFrame会自动加上索引(和Series一样),且全部列会被有序排列

    如果指定了列序列,则DataFrame就会按照指定顺序进行排列:

    In [55]: DataFrame(data,columns=['year','state','pop'])
    Out[55]:
       year    state  pop
    0  2000     onio  1.5
    1  2001     onio  1.7
    2  2002    linux  3.6
    3  2001    hp-ux  2.4
    4  2002  red-hat  2.9

    跟series一样如果传入在的列在数据中找不到,就会产生NA值:

    In [60]: frame2=DataFrame(data,columns=['year','state','pop','debt'],index=['one','two','three','four','five'])
    
    In [61]: frame2
    Out[61]:
           year    state  pop debt
    one    2000     onio  1.5  NaN
    two    2001     onio  1.7  NaN
    three  2002    linux  3.6  NaN
    four   2001    hp-ux  2.4  NaN
    five   2002  red-hat  2.9  NaN
    In [62]: frame2.columns
    Out[62]: Index(['year', 'state', 'pop', 'debt'], dtype='object')

    通过类似字典标记的方式或属性的方式,可以将DataFrame的列获取为一个Series:

    In [63]: frame2['state']
    Out[63]:
    one         onio
    two         onio
    three      linux
    four       hp-ux
    five     red-hat
    Name: state, dtype: object

    列可以通过赋值的方式进行修改,比如可以空的'debt'列赋值:

    In [73]: frame2
    Out[73]:
           year    state  pop debt
    one    2000     onio  1.5  NaN
    two    2001     onio  1.7  NaN
    three  2002    linux  3.6  NaN
    four   2001    hp-ux  2.4  NaN
    five   2002  red-hat  2.9  NaN
    
    In [74]: frame2['debt']=16.5
    
    In [75]: frame2
    Out[75]:
           year    state  pop  debt
    one    2000     onio  1.5  16.5
    two    2001     onio  1.7  16.5
    three  2002    linux  3.6  16.5
    four   2001    hp-ux  2.4  16.5
    five   2002  red-hat  2.9  16.5

    将列表或者数组赋值给某个列时,其长度必须跟DataFrame的长度向匹配。如果赋值的是一个Series,就会精确匹配DataFrame的索引,所有的空位都将会填上缺失值:

    In [76]: val=Series([-1.2,-1.3,-1.4],index=['two','four','five'])
    
    In [77]: frame2['debt']=val
    
    In [78]: frame2
    Out[78]:
           year    state  pop  debt
    one    2000     onio  1.5   NaN
    two    2001     onio  1.7  -1.2
    three  2002    linux  3.6   NaN
    four   2001    hp-ux  2.4  -1.3
    five   2002  red-hat  2.9  -1.4

    为不存在的列赋值,会创建出一个新列,关键字del可以删除列:

    In [79]: frame2['estarn']=frame2['state']=='onio'
    
    In [80]: frame2
    Out[80]:
           year    state  pop  debt  estarn
    one    2000     onio  1.5   NaN    True
    two    2001     onio  1.7  -1.2    True
    three  2002    linux  3.6   NaN   False
    four   2001    hp-ux  2.4  -1.3   False
    five   2002  red-hat  2.9  -1.4   False
    
    In [81]: del frame2['estarn']
    
    In [82]: frame2
    Out[82]:
           year    state  pop  debt
    one    2000     onio  1.5   NaN
    two    2001     onio  1.7  -1.2
    three  2002    linux  3.6   NaN
    four   2001    hp-ux  2.4  -1.3
    five   2002  red-hat  2.9  -1.4

    另一种常见的数据形式那就是嵌套字典(也就是字典的字典)

    In [85]: pop={'Nevda':{2001:2.4,2002:2.9},'Ohio':{2000:1.5,2001:1.7,2002:3.6}}
    
    In [87]: frame3=DataFrame(pop)
    
    In [88]: frame3
    Out[88]:
          Nevda  Ohio
    2000    NaN   1.5
    2001    2.4   1.7
    2002    2.9   3.6

    它被解释为:外层字典的键作为列,内层键作为行索引.

    当然也可以对结果进行转置:

    In [89]: frame3.T
    Out[89]:
           2000  2001  2002
    Nevda   NaN   2.4   2.9
    Ohio    1.5   1.7   3.6
  • 相关阅读:
    OneThink学习笔记02----数据字典(即OneThink项目数据库里的表及其字段)
    php获取checkbox复选框的内容
    OneThink学习笔记01
    解决jQuery UI 对话框兼容性问题
    安装Visual Studio 2013 出现0x80070643错误
    科幻小说网1.0构思
    数据结构之二分查找(PHP)
     兼容性问题
    【转】Apache的Order Allow,Deny 详解
    adb错误解决
  • 原文地址:https://www.cnblogs.com/catxjd/p/9164165.html
Copyright © 2020-2023  润新知