Find all-zero columns in pandas sparse matrix
<p>For example I have a coo_matrix A :</p>

<pre><code>import scipy.sparse as sp
A = sp.coo_matrix([3,0,3,0],
                  [0,0,2,0],
                  [2,5,1,0],
                  [0,0,0,0])
</code></pre>

<p>How can I get result [0,0,0,1], which indicates that first 3 columns contain non-zero values, only the 4th column is all zeros.</p>

<p>PS : cannot convert A to other type.<br>
PS2 : I tried using <code>np.nonzeros</code> but it seems that my implementation is not very elegant.</p>

<p>The actual logical operation can be performed like this:</p>

<pre><code>b = (A!=0).sum(axis=0)==0
# matrix([[False, False, False,  True]], dtype=bool)
</code></pre>

<p>Now, to ensure that I'm answering your question exactly, I'd better tell you how you <em>could</em> convert from booleans to integers (although really, for most applications I can think of, you can do a lot more in <code>numpy</code> and friends if you stick with an array of <code>bool</code>s):</p>

<pre><code>b = b.astype(int)
#matrix([[0, 0, 0, 1]])
</code></pre>

<p>Either way, to then convert from a <code>matrix</code> to a <code>list</code>, you could do this:</p>

<pre><code>c = list(b.flat)
# [0, 0, 0, 1]
</code></pre>

<p>...although again, I'm not sure this is the best thing to do: for most applications I can imagine, I would perhaps just convert to a one-dimensional <code>numpy.array</code> with <code>c = b.A.flatten()</code> instead.</p>

python numpy scipy sparse-matrix