Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy / pandas array comparison with multiple values in other array

Tags:

pandas

numpy

I have an array

a = np.arange(0, 100)

and another array with some cut-off points

b = np.array([5, 8, 15, 35, 76])

I want to create an array such that

c = [0, 0, 0, 0, 1, 1, 1, 2, 2, ..., 4, 4, 5]

Is there an elegant / fast way to do this? Possible in Pandas?

like image 307
Mike Avatar asked Jan 27 '23 00:01

Mike


1 Answers

Here's a compact way -

(a[:,None]>=b).sum(1)

Another with cumsum -

p = np.zeros(len(a),dtype=int)
p[b] = 1
out = p.cumsum()

Another with searchsorted -

np.searchsorted(b,a,'right')

Another with repeat -

np.repeat(range(len(b)+1),np.ediff1d(b,to_begin=b[0],to_end=len(a)-b[-1]))

Another with isin and cumsum -

np.isin(a,b).cumsum()
like image 61
Divakar Avatar answered Jan 30 '23 23:01

Divakar