Count number of occurences of specific number in numpy arrary in Excel sheet using python
<pre><code>import numpy as np

from openpyxl import load_workbook

wb = load_workbook('Book1.xlsx')
sheet_1 = wb.get_sheet_by_name('Sheet1')

x1 = np.zeros(sheet_1.max_row)

for i in range(0,sheet_1.max_row):

    x1[i]=sheet_1.cell(row=i+1, column=1).value

x2[i] = np.count_nonzero(x1[i])
collections.Counter(x2[i])[0]
</code></pre>

<p>I wanted to count the number of occurrence of 0 in the same column and add them up and find out the total number of element in the excel sheet.so the results expecting should be 3.It seems that numpy is unable to count. I have tried sum but it only adds up the element in the array. Appreciate any help from you guys. </p>

<p>Error Message: TypeError: 'numpy.float64' object is not iterable</p>

<p>Excel sheet:
5
5
3
3
4
5
5
0
0
0</p>

<p>A straight forward numpy-only solution to the <em>"counting occurence of a specific number in a numpy array"</em> question would be this:</p>

<pre><code># Note that I cast x1 to np.int because "arr==x" is not reliable with floats
x1=np.array([5,5,3,3,4,5,5,0,0,0],dtype=np.int)

#(x1==0) is boolean array that is True only where 0s are.
#summing that array gives the count of 0s
count_0=np.sum(x1==0)
count_5=np.sum(x1==5)

print("# of 0 : ",count_0)
print("# of 5 : ",count_5)
</code></pre>

<p>Output :</p>

<pre><code># of 0 :  3
# of 5 :  4 
</code></pre>

<p><strong>An other solution (the one you where seemingly trying implement)</strong> using <code>Counter</code> would read :</p>

<pre><code>count=collections.Counter(x1)
print("# of 0 : ",count[0])
print("# of 5 : ",count[5])
</code></pre>

python excel numpy