find first instance of a row in pandas dataframe matching a criterion
<p>I have a dataframe giving event time (in days) and a value associated with each event.  </p>

<p>sorry for placing this in as code snippet, not sure of any other way to show format as a table in this question.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>+-----------+----------+
| EventTime |  Value   |
+-----------+----------+
| 389.9067  | 0.076014 |
| 670.9632  | 0.190521 |
| 1012.2839 | 0.266599 |
| 1025.2452 | 0.355095 |
| 1347.1064 | 0.45189  |
| 3554.909  | 0.64213  |
| 3932.491  | 0.688693 |
| 4450.6369 | 0.730536 |
| 4819.5832 | 0.746905 |
| 6252.0017 | 0.880531 |
| 6951.3345 | 0.898307 |
| 7607.0877 | 0.945048 |
| 9044.0014 | 1.002455 |
| 9433.6679 | 1.083201 |
+-----------+----------+</code></pre>
</div>
</div>
</p>

<p>I am interested in obtaining the event time associated with the first value >= a given input, e.g. if input =0.40, I want to report 1347.1064</p>

<p>Ideally, I would like a general solution where I supply a list of value thresholds and the dataframe, e.g. (.4, .7, .9) and obtain back a list (or any other data structure) with the corresponding event times.</p>

<p>Looked around, did not see anything obvious in terms of a solution, but probably just missing something or my ignorance of pandas, trying to learn.</p>

<p>Thanks in advance</p>

<p>Here's one approach using <code>searchsorted</code> -</p>

<pre><code>df.EventTime[df.Value.searchsorted([.4,.7,.9])]
</code></pre>

<p>Sample run -</p>

<pre><code>In [281]: df
Out[281]: 
    EventTime     Value
0  333.690569  0.097736
1  942.624952  0.136822
2  211.588088  0.246093
3  514.476542  0.483235
4  650.769771  0.643968
5  457.457053  0.687587
6   10.519801  0.730046
7  692.091846  0.833983
8  210.612897  0.922743
9  512.066182  0.964927

In [282]: df.EventTime[df.Value.searchsorted([.4,.7,.9])]
Out[282]: 
3    514.476542
6     10.519801
8    210.612897
Name: EventTime, dtype: float64
</code></pre>

<p>If you need the <code>EvenTime</code> values as an array, use <code>df.EventTime.values</code> instead.</p>

python pandas