Getting n smallest in row in a panda dataframe
<p>My dataframe looks like this:</p>

<pre><code>            A  B  C  D
date                  
2015-01-01  1  1  2  3
2015-01-02  1  2  3  3
2015-01-03  1  2  1  3
2015-01-04  3  2  1  1
2015-01-05  3  2  2  1
2015-01-06  1  1  2  3
2015-01-07  1  2  3  3
2015-01-08  1  2  1  3
2015-01-09  3  2  1  1
2015-01-10  3  2  2  1
2015-01-11  3  2  2  1
</code></pre>

<p>Basic rule: I would like to identify the 2 smallest values for each row and set the values for these to 1. The other values should be set to 0. </p>

<p>Additional rules: </p>

<pre><code>1 1 1 1  should be 1 1 1 1
1 2 2 2  should be 1 1 1 1
1 2 2 3  should be 1 1 1 0
1 2 3 4  should be 1 1 0 0
</code></pre>

<p>I hope you can follow the rules and for the dataframe in the beginning the result should be</p>

<pre><code>date        A   B   C   D
2015-01-01  1   1   0   0
2015-01-02  1   1   0   0
2015-01-03  1   0   1   0
2015-01-04  0   0   1   1
2015-01-05  0   1   1   1
2015-01-06  1   1   0   0
2015-01-07  1   1   0   0
2015-01-08  1   0   1   0
2015-01-09  0   0   1   1
2015-01-10  0   1   1   1
2015-01-11  0   1   1   1
</code></pre>

<p><strong>EDIT</strong></p>

<p><em>"Maybe you want rank with method='first' I think this would work – EdChum"</em>
Result for the dataframe:</p>

<pre><code>            A  B  C  D
date                  
2015-01-01  1  2  3  4
2015-01-02  1  2  3  4
2015-01-03  1  3  2  4
2015-01-04  4  3  1  2
2015-01-05  4  2  3  1
2015-01-06  1  2  3  4
2015-01-07  1  2  3  4
2015-01-08  1  3  2  4
2015-01-09  4  3  1  2
2015-01-10  4  2  3  1
2015-01-11  4  2  3  1
</code></pre>