Count the digits in a Numpy array

0 votes
How do you Count The Number Of Times Each Value Appears In An Array Of
Integers?
[0, 5, 4, 0, 4, 4, 3, 0, 0, 5, 2, 1, 1, 9]
Answer should be array([4, 2, 1, 1, 3, 2, 0, 0, 0, 1]) which means 0 comes 4 times,
1 comes 2 times, 2 comes 1 time, 3 comes 1 time and so on.python
Apr 21 in Data Analytics by Debnath
• 120 points
103 views

1 answer to this question.

0 votes

Hey, @Sourav,

We can solve this with the collection module, which refers to count frequencies of all elements in the array link. 

import collections #Function to count frequency of each element it returns a dictionary data structure whose keys are array elements and values are their corresponding frequencies {0: 4,  1: 2,  2: 1,  3: 1]

def CountFrequency(arr):

    return collections.Counter(arr)  
if __name__ == "__main__": #Driver Function

 arr = [0, 5, 4, 0, 4, 4, 3, 0, 0, 5, 2, 1, 1, 9]

    freq = CountFrequency(arr)

for key, value in freq.iteritems():

        print key, " -> ", value

Output:

0-> 4

1->2

2->1

3->1

Hope this will help you.

answered Apr 21 by Gitika
• 29,290 points
Thanks Gitika,

It Works.

Related Questions In Data Analytics

0 votes
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 32,460 points
228 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
174 views
0 votes
1 answer
0 votes
1 answer

R function for finding the index of an element in a vector?

Yes, we can find the index of ...READ MORE

answered Apr 13, 2018 in Data Analytics by zombie
• 3,750 points
12,939 views
0 votes
2 answers

How can we count TRUE values in a logical vector?

Hi, You can get a count of all ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 32,460 points
4,931 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is size includes NaN ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points
973 views
0 votes
1 answer
0 votes
1 answer

How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

answered Apr 1, 2019 in Python by Pavan
633 views
0 votes
1 answer