Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get counts per each partition in the values ranging 0-10, 10-20,... 90-100

I have a data that is in the range of 1-100. I want to get count of this data per the following ranges. Let's say I have this data: [17, 30, 62 65, 92, 95, 98]. I want to obtain this:

00-10: 0
11-20: 1
21-30: 1
31-40: 0
41:50: 0
51:60: 0
61:70: 2
71:80: 0
81:90: 0
91:100: 3

I wonder if there is pandas/numpy/spicy function to achieve this fast. I appreciate any help!

like image 849
renakre Avatar asked Dec 02 '22 12:12

renakre


1 Answers

You can use cut with value_counts:

bins = np.arange(0,110,10)
s = pd.Series([17, 30, 62, 65, 92, 95, 98])
s1 = pd.cut(s, bins=bins)
print (s1.value_counts(sort=False))
(0, 10]      0
(10, 20]     1
(20, 30]     1
(30, 40]     0
(40, 50]     0
(50, 60]     0
(60, 70]     2
(70, 80]     0
(80, 90]     0
(90, 100]    3
dtype: int64
like image 176
jezrael Avatar answered Dec 08 '22 15:12

jezrael