Pandas dataframe, each cell into list - more pythonic way?
<p>I have a pandas dataframe with columns and rows like this:</p>

<pre><code>    a   b   c   d  
a  40  15  25  35  

b  10  25  35  45

c  20  35  45  55

d  40  45  55  65
</code></pre>

<p>For all numbers > 30 I need an output like this:</p>

<pre><code>a, a, 40
a, d, 40
b, c, 35
b, d, 45
</code></pre>

<p>and so on.</p>

<p>Currently I am running a loop like this:</p>

<pre><code>    for i in df.columns:
        for j in df.index:
            if df[i][j] &gt; 30:
                a.append(i+","+j+","+str(df[i][j])")
</code></pre>

<p>This works, but is very slow. Is there a more pythonic way to do this?</p>

<p>Thanks!</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.stack.html" rel="nofollow"><code>stack</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>df = df.stack().reset_index()
df.columns = ['a','b','c']

print (df[df.c &gt; 30])
    a  b   c
0   a  a  40
3   a  d  35
6   b  c  35
7   b  d  45
9   c  b  35
10  c  c  45
11  c  d  55
12  d  a  40
13  d  b  45
14  d  c  55
15  d  d  65
</code></pre>

<p>Similar solution:</p>

<pre><code>s = df.stack()
df = s[s &gt; 30].reset_index()
df.columns = ['a','b','c']

print (df)
    a  b   c
0   a  a  40
1   a  d  35
2   b  c  35
3   b  d  45
4   c  b  35
5   c  c  45
6   c  d  55
7   d  a  40
8   d  b  45
9   d  c  55
10  d  d  65
</code></pre>

<p>Another solution:</p>

<pre><code>df1 = df[df &gt; 30].stack().reset_index()
df1.columns = ['a','b','c']
df1.c = df1.c.astype(int)
print (df1)
    a  b   c
0   a  a  40
1   a  d  35
2   b  c  35
3   b  d  45
4   c  b  35
5   c  c  45
6   c  d  55
7   d  a  40
8   d  b  45
9   d  c  55
10  d  d  65
</code></pre>

<hr>

<p>Last you can <code>apply</code> join:</p>

<pre><code>df['d'] = df.astype(str).apply(', '.join, axis=1)
print (df)
    a  b   c         d
0   a  a  40  a, a, 40
1   a  d  35  a, d, 35
2   b  c  35  b, c, 35
3   b  d  45  b, d, 45
4   c  b  35  c, b, 35
5   c  c  45  c, c, 45
6   c  d  55  c, d, 55
7   d  a  40  d, a, 40
8   d  b  45  d, b, 45
9   d  c  55  d, c, 55
10  d  d  65  d, d, 65

print (df.d.tolist())
['a, a, 40', 'a, d, 35', 'b, c, 35', 'b, d, 45', 'c, b, 35', 'c, c, 45', 
'c, d, 55', 'd, a, 40', 'd, b, 45', 'd, c, 55', 'd, d, 65']
</code></pre>

python pandas