Python bisect and Pandas dataframe: KeyError


This Content is from Stack Overflow. Question asked by Kubra

I am using bisect for finding the first index that is greater than my value:

bisect.bisect_left(X['x'].iloc[116:231], x=0.15)

X is in ascending order and the first value X['x'].iloc[116] is 0.275. The bisect function above gives me KeyError: 57. I have understand that it cannot find the corresponding key but cannot understand why it tries to find the key ’57’. My expectation is that the first greater value than 0.15 is 0.275 and it should give me the index 116.

I have used bisect because my list is already sorted, but if there is more efficient way to find the first index greater than a specific value I would be pleasure if you let me know.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?