converting dataframe to list of tuples on condition
<p>I have following df:</p>

<pre><code>    1         2         3         4 
1  NaN  0.000000  0.000000  0.000000
2  NaN  0.027273  0.000000  0.000000
3  NaN  0.000000  0.101449  0.000000
4  NaN  0.000000  0.000000  0.194245
5  NaN  0.000000  0.000000  0.000000
6  NaN  0.000000  0.000000  0.000000
7  NaN  0.000000  0.000000  0.000000
8  NaN  0.000000  0.000000  0.000000
13 NaN  0.000000  0.000000  0.000000
14 NaN  0.000000  5         0.000000
</code></pre>

<p>How I can convert it to list of tuples <code>[(column, row, data)]</code> and to take only values that are greater then <code>zero</code>.</p>

<p>for example I want to have following values:</p>

<pre><code>[(2,2,0.027273), (3,3,0.101449 ), (3,14,5),(4,4,0.194245)]
</code></pre>

<p>You can first cast columns to <code>int</code> (if necessary), <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.unstack.html" rel="nofollow"><code>unstack</code></a> and use list comprehension, where is necessary convert first and second value in <code>tuples</code> to <code>int</code> (default is <code>float</code>):</p>

<pre><code>df.columns = df.columns.astype(int)

s = df.unstack()
tuples = [tuple((int(x[0]),int(x[1]),x[2])) for x in s[s&gt;0].reset_index().values]
print (tuples)
[(2, 2, 0.027273000000000002), (3, 3, 0.101449), (3, 14, 5.0), (4, 4, 0.194245)]
</code></pre>

python list pandas dataframe tuples