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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With