zoukankan      html  css  js  c++  java
  • pandas库的Series类型

      pandas:基础数据分析套件,交叉分析,时序分析,假设检验等等。pandas是为了解决数据分析任务而创建的一种基于Numpy的工具,它纳入了大量的库和一些标准的数据模型,提供了非常高效的操作大型数据集所需要的方法并且提供了大量的能使我们便捷的处理数据的函数,pandas的出现也使python成为最为广泛使用的强大而高效的数据分析环境之一。

      pandas的官网:http://pandas.pydata.org/  

      pandas的Series类型:

      Series类型由一组数据及与之相关的数据索引组成。

    自动索引:

    1 import pandas as pd
    2 a = pd.Series([9,8,7,6])
    3 a
    4 
    5 0    9
    6 1    8
    7 2    7
    8 3    6
    9 dtype: int64

     自定义索引:

    1 import pandas as pd
    2 a = pd.Series([9,8,7,6],index = ['a','b','c','d'])#可以省略掉index=
    3 a
    4 
    5 a    9
    6 b    8
    7 c    7
    8 d    6
    9 dtype: int64

     生成和创建Series类型的方法:

    1、从标量值创建

    1 import pandas as pd
    2 s = pd.Series(25,index=['a','b','c'])#不能省略index=
    3 s
    4 
    5 a    25
    6 b    25
    7 c    25
    8 dtype: int64

    2、从字典类型创建

    1 import pandas as pd
    2 d = pd.Series({'a':9,'b':8,'c':7})
    3 d
    4 
    5 a    9
    6 b    8
    7 c    7
    8 dtype: int64
    1 import pandas as pd
    2 e = pd.Series({'a':9,'b':8,'c':7},index=['c','a','b','d'])
    3 e
    4 
    5 c    7.0
    6 a    9.0
    7 b    8.0
    8 d    NaN
    9 dtype: float64

    3、从ndarray创建

     1 import pandas as pd
     2 import numpy as np
     3 n = pd.Series(np.arange(5))
     4 n
     5 
     6 0    0
     7 1    1
     8 2    2
     9 3    3
    10 4    4
    11 dtype: int32
     1 import pandas as pd
     2 import numpy as np
     3 m = pd.Series(np.arange(5),index=np.arange(9,4,-1))
     4 m
     5 
     6 9    0
     7 8    1
     8 7    2
     9 6    3
    10 5    4
    11 dtype: int32
    

     Series的基本操作:

     1 import pandas as pd
     2 b = pd.Series([9,8,7,6],['a','b','c','d'])
     3 print(b)
     4 print(b.index)
     5 print(b.values)
     6 
     7 5
     8 b.values
     9 a    9
    10 b    8
    11 c    7
    12 d    6
    13 dtype: int64
    14 Index(['a', 'b', 'c', 'd'], dtype='object')
    15 Out[19]:
    16 array([9, 8, 7, 6], dtype=int64)
     1 import pandas as pd
     2 b = pd.Series([9,8,7,6],['a','b','c','d'])
     3 print(b['b'])#自动索引和自定义索引并存
     4 print(b[1])
     5 print(b[['c','d',0]])#两套索引并存,但不能混用
     6 print(b[['c','d','a']])
     7 
     8 8
     9 8
    10 c    7.0
    11 d    6.0
    12 0    NaN
    13 dtype: float64
    14 c    7
    15 d    6
    16 a    9
    17 dtype: int64
     1 import pandas as pd
     2 b = pd.Series([9,8,7,6],['a','b','c','d'])
     3 b
     4 b[3]
     5 b[:3]#与ndarray不同,切片出来的为Series类型,既有索引又有值
     6 
     7 a    9
     8 b    8
     9 c    7
    10 dtype: int64
     1 b[b>b.median()]
     2 a    9
     3 b    8
     4 dtype: int64
     5 
     6 np.exp(b)
     7 a    8103.083928
     8 b    2980.957987
     9 c    1096.633158
    10 d     403.428793
    11 dtype: float64
    12 
    13 b['b']
    14 8
    15 
    16 'c'in b#判断是否在索引中
    17 True
    18 
    19 0 in b
    20 False
    21 
    22 b.get('f',100)#从b中提取索引f的值,f对应的值不存在返回100
    23 100

     Series类型对齐操作:

      Series+Series:

     1 import pandas as pd
     2 a = pd.Series([1,2,3],['c','d','e'])
     3 b = pd.Series([9,8,7,6],['a','b','c','d'])
     4 a+b
     5 
     6 a    NaN
     7 b    NaN
     8 c    8.0
     9 d    8.0
    10 e    NaN
    11 dtype: float64

       Series的name属性:

    Series对象和索引都可以有一个名字,存储在属性 .name中。

    1 import pandas as pd
    2 b = pd.Series([9,8,7,6],['a','b','c','d'])
    3 b.name#没有输出
     1 b.name = 'Series对象'
     2 b.index.name = '索引列'
     3 b
     4 
     5 索引列
     6 a    9
     7 b    8
     8 c    7
     9 d    6
    10 Name: Series对象, dtype: int64

     Series类型的修改:

    Series对象可以随时修改并即刻生效:

     1 b['a'] = 15
     2 b.name = 'Series'
     3 b
     4 
     5 索引列
     6 a    15
     7 b     8
     8 c     7
     9 d     6
    10 Name: Series, dtype: int64
     1 b.name = 'New Series'
     2 b['b','c'] = 20
     3 b
     4 
     5 索引列
     6 a    15
     7 b    20
     8 c    20
     9 d     6
    10 Name: New Series, dtype: int64

     Series类型

      Series是一维带“标签”数组。

        index_0   ——>  data_a

      Series基本操作类似ndarray和字典,但根据索引对齐。

  • 相关阅读:
    Android布局1
    QML 自定义折线图
    QML ChartView 画折线图
    操作系统复习笔记
    Redis的使用
    Git的基本使用
    Python json to excel/csv
    .NET中进行Base64加密解密
    用 IIS 7、ARR 與 Velocity 建设高性能的大型网站
    微信突然出现redirect_uri 参数错误
  • 原文地址:https://www.cnblogs.com/wxlblogs/p/7278163.html
Copyright © 2011-2022 走看看