pandas series can't get index

后端 未结 2 1246

not sure what the problem is here... all i want is the first and only element in this series

>>> a
1    0-5fffd6b57084003b1b582ff1e56855a6!1-AB87696         


        
2条回答
  •  醉话见心
    2020-12-21 10:18

    When the index is integer, you cannot use positional indexers because the selection would be ambiguous (should it return based on label or position?). You need to either explicitly use a.iloc[0] or pass the label a[1].

    The following works because the index type is 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
    

    But for integer index, things are different:

    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
    

提交回复
热议问题