Attain a tally of a column of a 2d array
<p>I have a 2d array data. And would like to attain a tally every time the jth iteration is a 1.
Where i = rows and j = columns. 
How do I go about doing this without a for loop? </p>

<p>Conceptually something like this:</p>

<pre><code>for r in range(row):
    if(data[r][j] == 1)
        amount += 1
</code></pre>

<p>I interprete this question such that you want to iterate over both, rows and columns, and add 1 to <code>amount</code> for each entry in data that is 1. This can be done without looping as follows.</p>

<pre><code>import numpy as np

data = np.ones((6,8))
amount = data[data == 1].sum()
print amount
</code></pre>

<p>If instaead, you fix one column <code>j</code>, and only want the amount in this column:</p>

<pre><code>import numpy as np
j=7
data = np.ones((6,8))
amount = data[:,j][data[:,j]==1].sum()
print amount
</code></pre>

python arrays numpy multidimensional-array scipy