locate the numeric position of a non numeric index value

后端 未结 3 653
小蘑菇
小蘑菇 2021-01-13 17:40

Consider the series s below:

s = pd.Series(np.arange(18, 0, -3), list(\'ABCDEF\'))
s

A    18
B    15
C    12
D     9
E     6
F     3
dtype: int         


        
相关标签:
3条回答
  • 2021-01-13 18:04

    Use np.searchsorted -

    np.searchsorted(s.index.values,'D')
    

    Or use the method, like so -

    s.index.searchsorted('D')
    
    0 讨论(0)
  • 2021-01-13 18:06

    You can use Index.get_loc:

    print(s.index.get_loc('D'))
    3
    
    0 讨论(0)
  • 2021-01-13 18:21
    m = s.index == 'D'
    idx = m.argmax() if m.any() else None
    
    0 讨论(0)
提交回复
热议问题