How to get calculations for each section in values ​​in the range 0-10, 10-20, ... 90-100

I have data that are in the range of 1-100. I want to get the amount of this data in the following ranges. Let's say I have this data: [17, 30, 62 65, 92, 95, 98]. I want to get 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 a pandas / numpy / spicy function to achieve this. I appreciate any help!

+4
source share
6 answers

You can use cutwith 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
+5
source

This is just a histogram, so np.histogram(data, np.arange(0,101,10))[0]

+5
source

, numpy.digitize numpy.bincount.

:

import numpy as np

a = np.array([9, 17, 30, 62, 65, 92, 95, 98])

bins = np.arange(0, 100, 10)
inds = np.digitize(a, bins) - 1

counts  = np.bincount(inds)
for r, count in zip(bins, counts):
    print((r, r+10), count)

. , . , . np.digitize i , bins[i-1] <= x < bins[i] , (bin 0).

+2

-numpy-, , ... collections.Counter :

from collections import Counter

a = [ 10,11,17, 30, 62, 65, 92, 95, 98,100]

# directly count using a generator comprehension instead of a loop
c = Counter(((i-1)//10)*10 for i in a)


for i in range(0,((max(a)+1)*10)//10,10):
    print("{}-{}: {}".format(i+1,i+10,c[i] if i in c else 0))

, 0, : , 0 .

( , 1, ):

1-10: 1
11-20: 2
21-30: 1
31-40: 0
41-50: 0
51-60: 0
61-70: 2
71-80: 0
81-90: 0
91-100: 4
101-110: 0
+2
source

You can achieve this with the built-in module bisectas follows:

from bisect import bisect

my_list = [17, 30, 62, 65, 92, 95, 98]  # sort it if not already sorted
my_interval = list(range(0, 101, 10))

new_list = [((i+1, j), len(my_list[bisect(my_list, i+1):bisect(my_list, j)])) \
    for i, j in zip(my_interval, my_interval[1:])]

The final hold value will be new_list:

[((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)]

To print the values ​​in the desired format, follow these steps:

for (i, j), val in new_list:
    print '{}-{}: {}'.format(i, j, val)

which will print:

1-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
+2
source
import numpy as np
# use np.where or np.nonzero for indices and np.logical_and to set alpha/omega
a = np.array([17, 30, 62, 65, 92, 95, 98])

for n in range(0,100,10):
  b = a[np.where(np.logical_and(a>=n, a<=n+10))]
  c = a[np.nonzero(np.logical_and(a>=n, a<=n+10))]
  print ((n, n+10), len(b), len(c), b, c)

(0, 10) 0 0 [] []
(10, 20) 1 1 [17] [17]
(20, 30) 1 1 [30] [30]
(30, 40) 1 1 [30] [30]
(40, 50) 0 0 [] []
(50, 60) 0 0 [] []
(60, 70) 2 2 [62 65] [62 65]
(70, 80) 0 0 [] []
(80, 90) 0 0 [] []
(90, 100) 3 3 [92 95 98] [92 95 98]
+2
source

Source: https://habr.com/ru/post/1666232/


All Articles