1

Suppose I have a Pandas Index object. I want to find the integer index of an item in the index. For example:

index = pd.Index( [ 'a', 'b', 'c' ] )
print np.argmax( index=='b' )
>>> 2

However, my solution involves an O(n) lookup, whereas I think there should be a way to find a O(1) lookup as this is an index.

Ginger
  • 8,320
  • 12
  • 56
  • 99
  • This is a duplicate of this question: http://stackoverflow.com/questions/17244049/finding-label-location-in-a-dataframe-index-pandas, was trying to find it – EdChum Aug 25 '15 at 09:06

1 Answers1

2

Use get_loc:

In [22]:
index = pd.Index( [ 'a', 'b', 'c' ] )
index.get_loc('b')

Out[22]:
1
EdChum
  • 376,765
  • 198
  • 813
  • 562