0

I have a numpy array ids = np.array([1,1,1,1,2,2,2,3,4,4]) and another array of equal length vals = np.array([1,2,3,4,5,6,7,8,9,10])

Note: the ids array is sorted by ascending order

I would like to insert 4 zeros before the beginning of each new id - i.e. new array = np.array([0,0,0,0,1,2,3,4,0,0,0,0,5,6,7,0,0,0,0,8,0,0,0,0,9,10])

Only, way I am able to produce this is by iterating through the array which is very slow - and I am not quite sure how to do this using insert, pad, or expand_dim ...

4
  • Do the ids always go in increasing order and continuously? Commented Jul 13, 2019 at 1:10
  • Yes they do - Sorry I didn't explicitly point this out Commented Jul 13, 2019 at 1:11
  • 1
    Make a big enough array of zeros, and copy your array to the right places. Commented Jul 13, 2019 at 2:07
  • Hey @hpaulj - was just about to try this approach Commented Jul 13, 2019 at 2:15

2 Answers 2

1

Since your ids increment and are continuous, this isn't so tough, gets a bit messy calculating the offsets however.


n = 4
m = np.flatnonzero(np.append([False], ids[:-1] != ids[1:]))

shape = vals.shape[0] + (m.shape[0]+1) * n
out = np.zeros(shape)

d = np.append([0], m) + np.full(m.shape[0] + 1, n).cumsum()
df = np.append(np.diff(d).cumsum(), [out.shape[0]])

u = tuple([slice(i, j) for i, j in zip(d, df)])

out[np.r_[u]] = vals

array([ 0.,  0.,  0.,  0.,  1.,  2.,  3.,  4.,  0.,  0.,  0.,  0.,  5.,
        6.,  7.,  0.,  0.,  0.,  0.,  8.,  0.,  0.,  0.,  0.,  9., 10.])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works great - I ended up producing a solution that is similar but makes use of pd.groupby to calculate the new indices ... what you shared is faster
0

u can use np.zeros and append it to your existing array like

newid=np.append(np.zeros((4,), dtype=int),ids)

Good Luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.