2017-07-13 168 views
2

不知道是什麼問題在這裏...我要的是這一系列熊貓系列無法獲得指數

>>> a 
1 0-5fffd6b57084003b1b582ff1e56855a6!1-AB8769635... 
Name: id, dtype: object 

>>> len (a) 
1 

>>> type(a) 
<class 'pandas.core.series.Series'> 

>>> a[0] 

Traceback (most recent call last): 
    File "<pyshell#1>", line 1, in <module> 
    a[0] 
    File "C:\Python27\lib\site-packages\pandas\core\series.py", line 601, in __getitem__ 
    result = self.index.get_value(self, key) 
    File "C:\Python27\lib\site-packages\pandas\core\indexes\base.py", line 2477, in get_value 
    tz=getattr(series.dtype, 'tz', None)) 
    File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4404) 
    File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4087) 
    File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:14031) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 765, in pandas._libs.hashtable.Int64HashTable.get_item (pandas\_libs\hashtable.c:13975) 
KeyError: 0L 

爲什麼不說工作的第一個也是唯一元素?以及如何獲得第一個元素?

+0

該指數是1,儘量'一個[1]' – gionni

回答

7

當索引是整數時,不能使用位置索引器,因爲選擇不明確(應該根據標籤還是位置返回?)。您需要明確使用a.iloc[0]或通過標籤a[1]

下工作,因爲索引類型的對象:

a = pd.Series([1, 2, 3], index=['a', 'b', 'c']) 

a 
Out: 
a 1 
b 2 
c 3 
dtype: int64 

a[0] 
Out: 1 

但對於整數指數,情況就不同了:

a = pd.Series([1, 2, 3], index=[2, 3, 4]) 

a[2] # returns the first entry - label based 
Out: 1 

a[1] # raises a KeyError 
KeyError: 1 
+1

感謝......這麼簡單...... – jason