pandas的索引对象用来保存坐标轴标签和其它元数据(如坐标轴名或名称)。构建一个Series或DataFrame时任何数组或其它序列标签在内部转化为索引:
#索引对象
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
obj=Series(range (3 ),index =['a' ,'b' ,'c' ])
index=obj.index
print (index) # Index(['a', 'b', 'c'], dtype='object')
print (index[1 :]) # Index(['b', 'c'], dtype='object')
# index[1]='d' #索引对象是不可变的,因此不能由用户改变
# print(index)
# Traceback (most recent call last):
# File "D:/pandas/day1/indexTest.py", line 12, in <module>
# index[1]='d'
# File "D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 1670, in __setitem__
# Index(['a', 'b', 'c'], dtype='object')
# Index(['b', 'c'], dtype='object')
# raise TypeError("Index does not support mutable operations")
# TypeError: Index does not support mutable operations
#索引对象的不可变性非常重要,这样它可以在数据结构中结构中安全的共享:
index=pd.Index(np.arange(3 ))
obj=Series([1 ,33 ,2.5 ],index =index)
index2=obj.index
print (index is index2) #true
下图是库中内建的索引类清单。通过一些开发努力,索引可以被子类化,来实现特定坐标轴索引功能。
每个索引都有许多关于集合逻辑的方法和属性,且能够解决它所包含的数据的常见问题。如下图: