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, 2020 in Data Analytics by Debnath
• 120 points
2,621 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, 2020 by Gitika
• 65,770 points
Thanks Gitika,

It Works.

Related Questions In Data Analytics

+1 vote
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
• 33,030 points
5,050 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
1,974 views
0 votes
1 answer
0 votes
2 answers

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

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
56,679 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
3,520 views
0 votes
1 answer
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,865 views
0 votes
3 answers

How to select rows in a range from dataframe?

This should do it integer_location = np.where(df.index == ...READ MORE

answered Dec 16, 2020 in Data Analytics by Roshni
• 10,480 points
15,177 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP