Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

count occurrence in array python

arr = np.array( [1, 2, 3, 2, 1, 2])
occ = np.count_nonzero(arr==0)
print(occ)
0
occ = np.count_nonzero(arr==1)
print(occ)
2
Comment

How to count the occurrence of certain item in an ndarray?

a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
dict(zip(unique, counts))

# {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
Comment

PREVIOUS NEXT
Code Example
Python :: python how to show package version 
Python :: django reverse function 
Python :: python get substring between strings 
Python :: python tqdm 
Python :: factorial of a number in python 
Python :: drop-trailing-zeros-from-decimal python 
Python :: python print format 
Python :: sklearn classifiers 
Python :: python exception 
Python :: install local package python 
Python :: python extract list from string 
Python :: python user input to tuple 
Python :: infinite while python 
Python :: new column with multiple conditions 
Python :: python csv writer row by row 
Python :: pandas group by include nan 
Python :: cors python 
Python :: python pandas in list 
Python :: how to create new header of a dataframe in python 
Python :: month name in python 
Python :: numpy copy array 
Python :: python terminal game 
Python :: make poetry env 
Python :: Program to find GCD or HCF of two numbers python 
Python :: insert column in a dataframe 
Python :: driver code in python 
Python :: np sum 
Python :: search for a word in pdf using python 
Python :: pandas loc condition 
Python :: os file size python 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =