美文网首页我爱编程
使用python进行数据分析<五>(pandas入门

使用python进行数据分析<五>(pandas入门

作者: 尽斩桃花三千 | 来源:发表于2018-05-18 19:17 被阅读25次

    pandas基于Numpy构建

    pandas 的数据结构介绍

    Series
    #Series类似一维数组的对象, 传入数组可以生成Series对象
    In [5]: obj = Series([4,5,6,0])
    
    In [7]: obj
    Out[7]: 
    0    4
    1    5
    2    6
    3    0
    dtype: int64
    #可以看到左边有默认的index于数据一一对应,可以通过index取数据
    In [8]: obj[0]
    Out[8]: 4
    
    #可以在初始化的时候,传入index数据,指定索引
    In [10]: obj2 = Series([4,45,5,6],index = ['b','a','c','d'])
    #既可以通过指定的索引取数据,也可以通过index取得数据
    In [12]: obj2['a']
    Out[12]: 45
    
    In [13]: obj2[0]
    Out[13]: 4
    
    #也可以通过索引或者index 取得Series中的一组值
    In [18]: obj2[[1,2]]
    Out[18]: 
    a    45
    c     5
    dtype: int64
    
    In [19]: obj2[['a','b']]
    Out[19]: 
    a    45
    b     4
    dtype: int64
    
    #Numpy数组运算,(如布尔型数组进行过滤,标量乘法,应用数学函数等)都会保留索引与值之间的联系
    In [20]: obj2[obj2 > 10]
    Out[20]: 
    a    45
    dtype: int64
    
    In [21]: obj2*2
    Out[21]: 
    b     8
    a    90
    c    10
    d    12
    dtype: int64
    
    
    #Series也可看做是定长的有序字典,很多字典通用的函数,在Series中也可以用
    In [23]: 'a' in obj2
    Out[23]: True
    #也可以通过字典创建Series
    In [26]: sdata = {'aaa':1232,'bbb':324,'ccc':433}
    
    In [27]: obj3 = Series(sdata)
    
    In [28]: obj3
    Out[28]: 
    aaa    1232
    bbb     324
    ccc     433
    dtype: int64
    
    #如果同时传入一个索引的数组,会根据索引取出对应的元素, 缺失的用NAN代替.
    
    In [29]: index_str = ['eee','ccc','aaa']
    In [30]: obj4 = Series(sdata,index_str)
    
    In [31]: obj4
    Out[31]: 
    eee       NaN
    ccc     433.0
    aaa    1232.0
    dtype: float64
    
    #Series的索引也可以通过赋值的方式进行修改
    
    In [39]: obj2.index = ['bbbb', 'baaaaaa', 'cccccc', 'dddddddddddddddddddd']
    
    In [40]: obj2
    Out[40]: 
    bbbb                     4
    baaaaaa                 45
    cccccc                   5
    dddddddddddddddddddd     6
    dtype: int64
    
    
    DataFrame

    相关文章

      网友评论

        本文标题:使用python进行数据分析<五>(pandas入门

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