• Series 入门(创建和增删改查)


    Series 是pandas两大数据结构中(DataFrame,Series)的一种。使用pandas 前需要将pandas 模块引入,因为Series和DataFrame用的次数非常多,所以将其引入本地命名空间中会更方便。

    1.  
      from pandas import Series, DataFrame
    2.  
      import pandas as pd

    因此,一般在代码中看到pd.,都指的是pandas。

    1.创建Series

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

           Series对象本质上是一个NumPy的数组,因此NumPy的数组处理函数可以直接对Series进行处理。但是Series除了可以使用位置作为下标存取元素之外,还可以使用标签下标存取元素,这一点和字典相似。每个Series对象实际上都由两个数组组成:

    index: 它是从NumPy数组继承的Index对象,保存标签信息。
    values: 保存值的NumPy数组。

    注意三点:

     1. Series是一种类似于一维数组(数组:ndarray)的对象

     2. 它的数据类型没有限制(各种NumPy数据类型)

     3. 它有索引,把索引当做数据的标签(key)看待,这样就类似字典了(只是类似,实质上市数组)

     4.Series同时具有数组和字典的功能,因此它也支持一些字典的方法

    创建数组,例如:

    1.  
      In [1]:arr=[1,2,3,4] #创建数组
    2.  
       
    3.  
      In [2]:arr
    4.  
      Out[2]: [1, 2, 3, 4]


    创建Series:

    1.  
      series_1=Series(arr)
    2.  
      series_1
    3.  
      Out[146]: 
    4.  
      0    1
    5.  
      1    2
    6.  
      2    3
    7.  
      3    4
    8.  
      dtype: int64
    9.  
      series_2=Series([1,2,3,4])
    10.  
      series_2
    11.  
      Out[148]: 
    12.  
      0    1
    13.  
      1    2
    14.  
      2    3
    15.  
      3    4
    16.  
      dtype: int64


    创建包含多种数据类型的Series:

    1.  
      series_3=Series([1,2,'3',4,'a']) <span style="font-family: Arial, Helvetica, sans-serif;">#包含数字和字符串</span>
    2.  
      series_3
    3.  
      Out[150]: 
    4.  
      0    1
    5.  
      1    2
    6.  
      2    3
    7.  
      3    4
    8.  
      4    a
    9.  
      dtype: object #类型变成了字符串

    2.Series索引

    Series创建后会自动生成索引,默认从0开始

    可以指定和修改索引

    1.  
      In [154]: series_4.index=['a','b','c']
    2.  
       
    3.  
      In [155]: series_4
    4.  
      Out[155]:
    5.  
      a 1
    6.  
      b 2
    7.  
      c 3

    修改索引除了这里的直接修改还有一个reindex()方法。

    3.Series增删改查

    Series创建后可以对数据进行增删改查

    3.1 增:

    Series的add()方法是加法计算不是增加Series元素用的。

    使用append连接其他Series

    3.2删:

    1.  
      In [162]: series_4.drop('a')
    2.  
      Out[162]:
    3.  
      b 2
    4.  
      c 3
    5.  
      dtype: int64

    3.3 改:

    1.  
      In [170]: series_4['a']=4
    2.  
       
    3.  
      In [171]: series_4
    4.  
      Out[171]:
    5.  
      a 4
    6.  
      b 2
    7.  
      c 3
    8.  
      dtype: int64

    3.4 查:

    通过索引查单值

    1.  
      In [172]: series_4['a']
    2.  
      Out[172]: 4

    通过索引序列查多值:

    1.  
      series_4[['a','b']]
    2.  
      Out[174]:
    3.  
      a 4
    4.  
      b 2
    5.  
      dtype: int64

    通过布尔类型索引筛选:

    1.  
      In [175]: series_4[series_4>2]
    2.  
      Out[175]:
    3.  
      a 4
    4.  
      c 3
    5.  
      dtype: int64

    通过位置切片和标签切片查询数据:

    1.  
      series_4
    2.  
      Out[194]:
    3.  
      a 4
    4.  
      b 2
    5.  
      c 3
    6.  
      dtype: int64
    7.  
       
    8.  
      series_4[:2]
    9.  
      Out[195]:
    10.  
      a 4
    11.  
      b 2
    12.  
      dtype: int64
    13.  
       
    14.  
      series_4['a':'c']
    15.  
      Out[196]:
    16.  
      a 4
    17.  
      b 2
    18.  
      c 3
    19.  
      dtype: int64

    4.通过字典创建Series

    1.  
      series_5=Series({'a':1,'b':2,'c':3})
    2.  
       
    3.  
      series_5
    4.  
      Out[201]:
    5.  
      a 1
    6.  
      b 2
    7.  
      c 3
    8.  
      dtype: int64
  • 相关阅读:
    对称加密算法在C#中的踩坑日常
    php与Git下基于webhook的自动化部署
    1024程序员节阿里谜题解析
    一次ajax请求导致status为canceled的原因小记
    LigerUI下拉选择列表LigerComboBox中tree的节点初始化默认选中的问题
    linux环境下安装PHP扩展swoole
    Memcache PHP 使用笔记
    VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案
    mysql表的完整性约束
    mysql支持的数据类型
  • 原文地址:https://www.cnblogs.com/zknublx/p/9706355.html
Copyright © 2020-2023  润新知