python的pandas库里的数据结构介绍

首先介绍第一类:Series (注意大小写)

defination:Series is a one-dimensional labeled array capable of holding any data type(integers,strings,floating point numbers,python objects,etc.)

定义:Series是一维带标签的数组,其可包含任意类型的元素(整数,字符串,浮点数,Python对象等)

<1>创建Series对象的典型方法是:

import pandas as pd
import numpy as np
dic={'a':1,'b':2,'c':3}
p1=pd.Series(dic,index=['b','a','d','c','f'])
print(p1)


output is:
b     2
a     1
d   NaN
c     3
f   NaN
dtype: float64


p2=pd.Series([1,2],index=['apple','orange'])
print(p2)
>>>
apple     1
orange    2
dtype: int64


p3=pd.Series(6,index=range(3))
print(p3)
>>>
0    6
1    6
2    6
dtype: int64


<2>关于Series对象的操作

  1. 对Series的操作非常类似于对ndarray的操作
            如:
            import pandas as pd
            import numpy as np
            dic={'a':1,'b':2,'c':3}
            p1=pd.Series(dic,index=['b','a','d','c','f'])
            print(p1)


            output is:
            b     2
            a     1
            d   NaN
            c     3
            f   NaN
           dtype: float64

         2.对Series的操作类似于对字典的操作,可通过索引值来设置。
            如:
            print(s['a']) 
           >>>
           -0.325154630498
         
           s['e']=12
           print(s)
           >>>
           a     1.221680
           b     0.676150
           c    -0.966844
           d     1.123409
           e    12.000000
           dtype: float64

           print('e' in s)
           >>>
           True

          print(s['f'])
          >>>
          KeyError:'f'

          可采用get()方法,对于调用没有的标签返回值为None或者特殊的默认值,如
          print(s.get('f'))
          >>>
         None

         print(s.get('f',np.nan))
         >>>
         nan
         
        3.Series的向量化操作和标签调整
           如:
          print(s+s)
          >>>
          a     1.515210
          b     1.236786
          c    -1.338008
          d    -2.445264
          e    24.000000
          dtype: float64
         
        print(s*2)
        >>>
        a     4.028001
        b     1.056986
        c    -2.799877
        d    -3.421514
        e    24.000000
        dtype: float64

Series与ndarray的一个关键区别是对Series进行操作时,根据标签数据进行自动调整。因此,无需考虑序列是否有相同的标签。
(A key difference between Series and ndarray is that operations between Series antomatically align the data based on label. Thus, you can write computations without giving consideration to whether the Series involved have the same labels.)

        如:
        s1=s[1:]+s[:-1]
        print(s1)
        >>>
        a         NaN
        b   -1.721291
        c    0.848257
        d    0.410060
        e         NaN
        dtype: float64


注意:平时处理数据时,为了避免信息损失,对Series无需去掉数据缺失的样本。若是想去掉Series对象中的缺失数据,可采用dropna().
(Having an index label,though the data is missing, is typically importtant information sa part of a computation.)
       
         print(s1.dropna())
         >>>
         b   -1.145174
         c    0.802023
         d    1.354568
         dtype: float64
        4.命名属性(name attribute)

         如:
         s=pd.Series(np.random.randn(5),name='something')
         print(s)
         >>>
         0    0.921333
         1   -0.736353
         2   -1.202390
         3   -0.101308
         4    0.017583
         Name: something, dtype: float64
      

          print(s.name)

          >>>

          'something'

对序列Series重命名可采用pandas.Series.rename()

       

         s2=s.rename('difference')

         print(s2.name)

         >>>

         'difference'

注意:s和s2是不同的对象。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值