1

I have a 2D NumPy array:

>>> import numpy as np
>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

and a 1D array:

>>> b = np.arange(3)
>>> b
array([0, 1, 2])

Is there an elegant way to insert b into a as a new first column?

So that:

>>> a
array([[0, 1, 2, 3],
       [1, 4, 5, 6],
       [2, 7, 8, 9]])

1 Answer 1

2

You could use column_stack()

In [256]: np.column_stack((b, a))
Out[256]:
array([[0, 1, 2, 3],
       [1, 4, 5, 6],
       [2, 7, 8, 9]])
Sign up to request clarification or add additional context in comments.

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.