4

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?

1
  • does c start with 4 zeros or 5? Commented Jul 25, 2019 at 15:31

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.