不确定这里的问题是什么......我想要的只是本系列中的第一个也是唯一的元素
>>> 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 :(得分:8)
当索引是整数时,你不能使用位置索引器,因为选择是不明确的(如果它根据标签或位置返回?)。您需要明确使用a.iloc[0]
或传递标签a[1]
。
以下是有效的,因为索引类型是object:
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 :(得分:1)
查看以下代码:
import pandas as pd
import numpy as np
data1 = pd.Series(['a','b','c'],index=['1','3','5'])
data2 = pd.Series(['a','b','c'],index=[1,3,5])
print('keys data1: '+str(data1.keys()))
print('keys data2: '+str(data2.keys()))
print('base data1: '+str(data1.index.base))
print('base data2: '+str(data2.index.base))
print(data1['1':'3']) # Here we use the dictionary like slicing
print(data1[1:3]) # Here we use the integer like slicing
print(data2[1:3]) # Here we use the integer like slicing
keys data1: Index(['1', '3', '5'], dtype='object')
keys data2: Int64Index([1, 3, 5], dtype='int64')
base data1: ['1' '3' '5']
base data2: [1 3 5]
1 a
3 b
dtype: object
3 b
5 c
dtype: object
3 b
5 c
dtype: object
对于data1,索引的dtype是object,对于data2,它的int64。他写了一篇关于Jake VanderPlas的《数据科学手册》的书:“ Series对象在许多方面都像一维NumPy数组,在许多方面都像标准的Python字典一样”。因此,如果像data1一样索引是“对象”类型,则我们可以通过两种不同的方式来访问值:
1.通过字典进行切片/索引:
data1['1','3'] --> a,b
按整数进行切片/索引:
data1[1:3] --> b,c
如果在数据2的情况下索引dtype的类型为int64,则pandas没有机会决定是否要像切片/索引一样拥有索引或字典,因此它默认为像切片/索引一样索引,因此对data2 [1:3]当我们选择像切片/索引这样的整数时,我们得到的b,c就像data1一样。
尽管如此,VanderPlas提到在这种情况下要记住一件事:
“请注意,当您对显式索引(即data ['a':'c'])进行切片时,最终索引将包含在
切片,而当您使用隐式索引切片时(即data [0:2]),最终索引将从切片中排除。[...]这些切片和索引约定可能会引起混淆。 ”
为了克服这种混淆,您可以将loc用于基于标签的切片/索引,将iloc用于基于索引的切片/索引
喜欢:
import pandas as pd
import numpy as np
data1 = pd.Series(['a','b','c'],index=['1','3','5'])
data2 = pd.Series(['a','b','c'],index=[1,3,5])
print('data1.iloc[0:2]: ',str(data1.iloc[0:2]),sep='\n',end='\n\n')
# print(data1.loc[1:3]) --> Throws an error bacause there is no integer index of 1 or 3 (these are strings)
print('data1.loc["1":"3"]: ',str(data1.loc['1':'3']),sep='\n',end='\n\n')
print('data2.iloc[0:2]: ',str(data2.iloc[0:2]),sep='\n',end='\n\n')
print('data2.loc[1:3]: ',str(data2.loc[1:3]),sep='\n',end='\n\n') #Note that contrary to usual python slices, both the start and the stop are included
data1.iloc[0:2]:
1 a
3 b
dtype: object
data1.loc["1":"3"]:
1 a
3 b
dtype: object
data2.iloc[0:2]:
1 a
3 b
dtype: object
data2.loc[1:3]:
1 a
3 b
dtype: object
因此data2.loc [1:3]明确搜索索引中的1和3的值,并返回它们之间的值,而data2.iloc [0:2]返回索引中第零个元素之间的值。索引和索引中的第二个元素(不包括第二个元素)。