Pandas: How to find the first valid column among a series of columns
<p>I have a dataset of different sections of a race in a pandas dataframe from which I need to calculate certain features. It looks something like this:</p>

<pre><code>id         distance     timeto1000m    timeto800m    timeto600m   timeto400m   timeto200m    timetoFinish
1          1400m        10             21            30           39           50            60    
2          1200m        0              19            31           42           49            57   
3          1800m        0              0             0            38           49            62   
4          1000m        0              0             29           40           48            61
</code></pre>

<p>So, what I need to do is for each row find the first <code>timetoXXm</code> column that is non-zero and the correspoding distance <code>XX</code>. For instance, for <code>id=1</code> that would be 1000m, for <code>id=3</code> that would be 400m etc. </p>

<p>I can do this with a series of <code>if..elif..else</code> conditions but was wondering if there is a better way of doing this kind of lookup in pandas/numpy?</p>

<p>use <code>idxmax(1)</code></p>

<pre><code>df.set_index(['id', 'distance']).ne(0).idxmax(1)

id  distance
1   1400m       timeto1000m
2   1200m        timeto800m
3   1800m        timeto400m
4   1000m        timeto600m
dtype: object
</code></pre>

python pandas numpy feature-extraction