Mike
Mike

Reputation: 4259

numpy / pandas array comparison with multiple values in other array

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?

Upvotes: 4

Views: 61

Answers (2)

Divakar
Divakar

Reputation: 221624

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

Upvotes: 4

BENY
BENY

Reputation: 323326

Here is one way cut

pd.cut(a,[-np.Inf]+b.tolist()+[np.Inf]).codes
Out[383]: 
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
       4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
       4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
       5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], dtype=int8)

Upvotes: 4

Related Questions