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>Convert to an array or dense matrix, sum the absolute value along the first axis, test the result against zero, convert to int</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; (np.sum(np.absolute(a.toarray()), 0) == 0) * 1
array([0, 0, 0, 1])
&gt;&gt;&gt; (np.sum(np.absolute(a.todense()), 0) == 0) * 1
matrix([[0, 0, 0, 1]])
&gt;&gt;&gt; 
&gt;&gt;&gt; np.asarray((np.sum(np.absolute(a.todense()), 0) == 0), dtype = np.int32)
array([[0, 0, 0, 1]])
&gt;&gt;&gt;
</code></pre>

<hr>

<p>The first is the fastest - 24 uS for your example on my machine.</p>

<p>For a matrix made with <code>np.random.randint(0,3,(1000,1000))</code>, all are right at 25 mS on my machine.</p>

python numpy scipy sparse-matrix