0

I need to concatenate two numpy arrays side by side

np1=np.array([1,2,3])
np2=np.array([4,5,6])

I need np3 as [1,2,3,4,5,6] with the same shape, how to achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bare
  • 15
  • 7
  • 2
    [np.concatenate](https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html) works. – ssp Jul 23 '20 at 03:18
  • 1
    Does this answer your question? [Concatenating two one-dimensional NumPy arrays](https://stackoverflow.com/questions/9236926/concatenating-two-one-dimensional-numpy-arrays) – Bedir Yilmaz Jul 23 '20 at 04:01

2 Answers2

2

In concatenate you have to pass axis as None

In [9]: np1=np.array([1,2,3])
   ...: np2=np.array([4,5,6])

In [10]: np.concatenate((np1,np2), axis=None)
Out[10]: array([1, 2, 3, 4, 5, 6])
bigbounty
  • 16,526
  • 5
  • 37
  • 65
0

Use np.hstack made for this:

np3 = np.hstack((np1,np2))

output:

[1 2 3 4 5 6]
Ehsan
  • 12,072
  • 2
  • 20
  • 33