0

I have arrays like

arr1['a'] = np.array([1, 1, 1])
arr1['b'] = np.array([1, 1, 1])
arr1['c'] = np.array([1, 1, 1])

b_index = [0, 2, 5]

arr2['a'] = np.array([2, 2, 2, 2, 2, 2])
arr2['b'] = np.array([2, 2, 2, 2, 2, 2])
arr2['c'] = np.array([2, 2, 2, 2, 2, 2])
arr2['f'] = np.array([2, 2, 2, 2, 2, 2])

b_index is the list of indexes. I want to copy from arr1 to arr2 at indexes in b_index. so the result should be something like

arr2['a'] = np.array([1, 2, 1, 2, 2, 1])
arr2['b'] = np.array([1, 2, 1, 2, 2, 1])
arr2['c'] = np.array([1, 2, 1, 2, 2, 1])
arr2['f'] = np.array([2, 2, 2, 2, 2, 2])

I can obviously do using loops, but not sure if that is a right way to do that. We are talking about 100 columns('a','b','c') and around a 1 million rows.

1 Answer 1

1

One solution, which might not be optimal, is to use advanced array indexing:

In [1]: arr = np.ones((5, 3))

In [2]: arr2 = np.full((5, 5), 2)

In [3]: arr2[:, [1, 2, 4]] = arr

In [4]: arr2
Out[4]:
array([[2, 1, 1, 2, 1],
       [2, 1, 1, 2, 1],
       [2, 1, 1, 2, 1],
       [2, 1, 1, 2, 1],
       [2, 1, 1, 2, 1]])

Does it help ?

Sign up to request clarification or add additional context in comments.

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.