1

Given these two numpy arrays:

# Two 2-dim arrays with same row number but differnt column
a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]], dtype=np.int64)
a2 = np.array([[3],[3],[3]], dtype=np.int64)

How would one merge these to arrays two create a third array like the following:

# Third array with same row numbers as above two arrays but its column number is the sum of column numbers of the above two arrays
[[9,9,9,9,3],
 [9,9,9,9,3],
 [9,9,9,9,3]]

In simpler terms, how do you concatenate a column to a 2-dimensional array?

1
  • 3
    np.concatenate([a1,a2], 1) Commented Feb 28, 2020 at 19:46

1 Answer 1

1
import numpy as np

a1 = np.array([[9,9,9,9], [9,9,9,9],[9,9,9,9]])
a2 = np.array([[3],[3],[3]])
print(np.concatenate((a1, a2), axis=1))

https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html

Join a sequence of arrays along an existing axis.

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.