zoukankan      html  css  js  c++  java
  • pandas基础--数据结构:索引对象

    pandas索引对象负责管理轴标签和其他元数据(比如轴名称)。构建Series或DataFrame时,所用到的任何数组或其他序列的标签都会被转换成一个index。

     1 >>> obj = pd.Series(range(3), index=['a', 'b', 'c']) 
     2 >>> index = obj.index
     3 >>> index
     4 Index(['a', 'b', 'c'], dtype='object')
     5 >>> index[1:] 
     6 Index(['b', 'c'], dtype='object')
     7 >>> index[1] = 'd'  #Index对象不可更改
     8 Traceback (most recent call last):
     9   File "<stdin>", line 1, in <module>
    10   File "C:Python37libsite-packagespandascoreindexesase.py", line 3910, in __setitem__  
    11     raise TypeError("Index does not support mutable operations")
    12 TypeError: Index does not support mutable operations
    13 >>> index = pd.Index(np.arange(3))
    14 >>> obj2 = pd.Series([1.5, -2.5, 0], index=index) 
    15 >>> obj2.index is index
    16 True

    下表是pandas库内置的Index类。

    说明

    Index

    最泛化的Index对象,将轴标签表示为一个由python对象组成的NumPy数组

    Int64Index

    针对整数的特殊Index

    MultiIndex

    “层次化”索引对象,表示单个轴上的多层索引,可以看作由元数组组成的数组

    DatetimeIndex

    存储纳秒级时间戳(用NumPy的datatime64类型表示)

    PeriodIndex

    针对Period数据(时间间隔)的特殊Index

    除了长得像数组,Index的功能类似一个固定大小的集合。

     1 >>> frame3
     2 state  Nevada  Ohio
     3 year
     4 2001      2.4   1.7
     5 2002      2.9   3.6
     6 2000      NaN   1.5
     7 >>> 'Ohio' in frame3.columns
     8 True
     9 >>> 2003 in frame3.index    
    10 False

    每个索引都有一些方法和属性,它们可用于设置逻辑并回答有关该索引所包含的数据的常见问题。

    方法

    说明

    append

    连接另一个Index对象,产生一个新的Index

    diff

    计算差集,并得到一个Index

    intersection

    计算交集

    union

    计算并集

    isin

    计算一个指示各值是否都包含在参数集合中的布尔型数组

    delete

    删除索引i处的元素,并得到新的Index

    drop

    删除传入的值,并得到新的Index

    insert

    将元素插入到索引i处,并得到新的Index

    is_monotonic

    当各元素均大于等于前一个元素时,返回True

    is_unique

    当Index没有重复值时,返回True

    unique

    计算Index中唯一值的数组

  • 相关阅读:
    Codeforces 1000C Covered Points Count
    Array类型
    Object对象
    变量、作用域与内存
    window.onload 方法脚本
    页面的性能优化
    node属性
    JavaScript图片
    DOM节点
    canvas象棋 画图
  • 原文地址:https://www.cnblogs.com/mrlayfolk/p/12252494.html
Copyright © 2011-2022 走看看