pandas学习笔记3---索引

pandas的索引负责管理其轴标签和其他元数据内容,无论构建Series还是DataFrame时所用到的任何数据结果的列标签都会被转成一个index索引对象

index对象是不可修改的,因此用户不能对其进行修改,由Index方法创建的index与DataFrame.index以及Series.index具有相同的属性

frame1.diff(axis='index')
Out[234]: 
state   year  pop
number           
one      NaN  NaN
two      1.0  0.2
three    1.0 -0.5
four    -1.0  0.6
five     1.0 -0.4

index=pd.Index(range(4))

index
Out[236]: RangeIndex(start=0, stop=4, step=1)

index[0]=1
Traceback (most recent call last):

  File "<ipython-input-237-bf1c7bc3e531>", line 1, in <module>
    index[0]=1

  File "f:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2050, in __setitem__
    raise TypeError("Index does not support mutable operations")

TypeError: Index does not support mutable operations

pandas主要的对象有几类:

Index   将周标签标识为一个PYTHON对象组成的numpy数组

Int64Index 针对整数的特殊Index

MultiIndex  层次化索引对象,标识单轴上的多层索引,可以看做由元组组成的数组

DatetimeIndex 存储纳秒级时间戳

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

Index的方法和属性

append   连接一个对象,产生一个新的Index,此append非列表的元素添加,而是连接

diff     计算交集,其计算对象为DataFrame   Series,其函数形式为:DataFrame(diff(self, periods=1, axis=0)),Series(diff(self, periods=1))

Index的方法和属性有很多,没有必要全部去挨个学习,在用到相应的方法和属性时去查找相应的说明即可。

附Index的pandas.Index 官网API

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.html

猜你喜欢

转载自blog.csdn.net/walking_visitor/article/details/81125772