当索引为整数时,您将无法使用位置索引器,因为选择将是模棱两可的(应基于标签还是位置返回?)。您需要明确使用
a.iloc[0]或传递标签
a[1]。
由于索引类型是对象,因此可以进行以下操作:
a = pd.Series([1, 2, 3], index=['a', 'b', 'c'])aOut: a 1b 2c 3dtype: int64a[0]Out: 1
但是对于整数索引,情况有所不同:
a = pd.Series([1, 2, 3], index=[2, 3, 4])a[2] # returns the first entry - label basedOut: 1a[1] # raises a KeyErrorKeyError: 1



