首页 > 编程笔记

Pandas Series对象索引

Series的索引方法有以下3种。

1. 常见的Series索引方式

第一种方法,与 list 和 Numpy 中的一维数组的索引方法不相同,但形式相同。需要注意的是,此时的值不是 Numpy 中对应概念的下标,而是 Pandas 中的索引值,所以不能出现负数。否则将出现报错。示例代码如下。
In [1]: import pandas as pd
In [2]: a=pd.Series([0,1,2,3,4,5])
In [3]: a[0]  # 索引为0的元素
Out[3]: 0
In [4]: a[1:3]  # 索引从1到3的片段,不包含3
Out[4]: 
1    1
2    2
dtype: int64
In [5]: a[2:]  # 索引从2到末端的片段
Out[5]: 
2    2
3    3
4    4
5    5
dtype: int64
In [6]: a[1:5:2]  # 索引从1到5的片段,不包含5,且步长为2
Out[6]: 
1    1
3    3
dtype: int64
索引为负数报错信息入校所示:
 In [7]: a[-1]  # 索引为-1的元素
Traceback (most recent call last):
  File "<ipython-input-7-518d9f157c69>", line 1, in <module>
     a[-1]  # 索引为-1的元素
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", 
result = self.index.get_value(self, key)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", 
line 3103, in get_value
     tz=getattr(series.dtype, 'tz', None))
  File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_
value
  File "pandas\_libs\index.pyx", line 114, in pandas._libs.index.IndexEngine.get_
value
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_
loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.
hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 964, in pandas._libs.
hashtable.Int64HashTable.get_item
KeyError: -1

2. 使用.loc方式进行索引

第二种方法,与 list 和 Numpy 中一维数组的索引方法不相同,形式也不同,要使用 .loc,同样也不能出现负数。示例代码如下。
In [1]: import pandas as pd
In [2]: a=pd.Series([0,1,2,3,4,5])
In [3]: a.loc[0]  # 索引为0的元素
Out[3]: 0
In [4]: a.loc[1:3]  # 索引从1到3的片段,不包含3
Out[4]: 
1    1
2    2
3    3
dtype: int64
In [5]: a.loc[2:]  # 索引从2到末端的片段
Out[5]: 
2    2
3    3
4    4
5    5
dtype: int64
In [6]: a.loc[1:5:2]  # 索引从1到5的片段,不包含5,且步长为2
Out[6]: 
1    1
3    3
5    5
dtype: int64
若此时索引为负数,也会产生以下报错信息:
In [7]: a.loc[-1]  # 索引为-1的元素
Traceback (most recent call last):
  File "<ipython-input-7-d78ac491c510>", line 1, in <module>
    a.loc[-1]  # 索引为-1的元素
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line
1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line
1911, in _getitem_axis
    self._validate_key(key, axis)  
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line
1798, in_validate_key
    error()

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line
1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [-1] is not in the [index]'

3. 使用 .iloc方式进行索引

第三种方法,与 list 和 Numpy 中一维数组的索引方法相同,但形式不同,要使用 .iloc,此时可以出现负数索引。示例代码如下。
In [1]: import pandas as pd
In [2]: a=pd.Series([0,1,2,3,4,5])
In [3]: a.iloc[0]  # 索引为0的元素
Out[3]: 0
In [4]: a.iloc[1:3]  # 索引从1到3的片段,不包含3
Out[4]: 
1    1
2    2
dtype: int64
In [5]: a.iloc[2:]  # 索引从2到末端的片段
Out[5]: 
2    2
3    3
4    4
5    5
dtype: int64
In [6]: a.iloc[1:5:2]  # 索引从1到5的片段,不包含5,且步长为2
Out[6]: 
1    1
3    3
dtype: int64
In [7]: a.iloc[-1]  # 索引为-1的元素
Out[7]: 5
另外 Pandas 还提供了 at 和 iat 方法,这两个方法只取值,所以速度很快。at 对应 loc,iat 对应 iloc。

优秀文章