Iterate through the rows of a dataframe and reassign minimum values by group
<p>I am working with a dataframe that looks like this.</p>

<pre><code>  id time diff
0 0   34   nan
1 0   36   2
2 1   43   7
3 1   55   12
4 1   59   4
5 2   2    -57
6 2   10   8
</code></pre>

<p>What is an efficient way find the minimum values for 'time' by id, then set 'diff' to nan at those minimum values. I am looking for a solution that results in:</p>

<pre><code>   id time diff
0 0   34   nan
1 0   36   2
2 1   43   nan
3 1   55   12
4 1   59   4
5 2   2    nan
6 2   10   8
</code></pre>

<p><code>groupby('id')</code> and use <code>idxmin</code> to find the location of minimum values of <code>'time'</code>.  Finally, use <code>loc</code> to assign <code>np.nan</code></p>

<pre><code>df.loc[df.groupby('id').time.idxmin(), 'diff'] = np.nan
df
</code></pre>

<p><a href="http://i.stack.imgur.com/qsTwZ.png"><img src="http://i.stack.imgur.com/qsTwZ.png" alt="enter image description here"></a></p>

python pandas