Back to NumPy Notes
Topic #12

NumPy Array Join


Joining NumPy Arrays

Joining means putting contents of two or more arrays in a single array.

In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.

We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis. If axis is not explicitly passed, it is taken as 0.

Example

  import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4,
  5, 6])

arr = np.concatenate((arr1, arr2))

print(arr)

Example

  import numpy as np

arr1 = np.array([[1, 2], [3, 4]])

arr2 =
  np.array([[5, 6], [7, 8]])

arr = np.concatenate((arr1, arr2), axis=1)

print(arr)

Joining Arrays Using Stack Functions

Stacking is same as concatenation, the only difference is that stacking is done along a new axis.

We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.

We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0.

Example

  import numpy as np

arr1 = np.array([1, 2, 3])

arr2 =
  np.array([4, 5, 6])

arr = np.stack((arr1, arr2), axis=1)

print(arr)

Stacking Along Rows

NumPy provides a helper function: hstack() to stack along rows.

Example

  import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4,
  5, 6])

arr = np.hstack((arr1, arr2))

print(arr)

Stacking Along Columns

NumPy provides a helper function: vstack() to stack along columns.

Example

  import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4,
  5, 6])

arr = np.vstack((arr1, arr2))

print(arr)

Stacking Along Height (depth)

NumPy provides a helper function: dstack() to stack along height, which is the same as depth.

Example

  import numpy as np

arr1 = np.array([1, 2, 3])

arr2 = np.array([4,
  5, 6])

arr = np.dstack((arr1, arr2))

print(arr)

Want to go beyond the notes?

Join Coding Now Tech Institute's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

NumPy Array Join – FAQs

Quick answers about learning NumPy Array Join in NumPy.

This free note from Coding Now Tech Institute explains NumPy Array Join in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on Coding Now Tech Institute, including NumPy Array Join, is 100% free with no signup required.
With focused practice, most students grasp NumPy Array Join in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now