NumPy Joining Array

🔗 NumPy Joining Arrays

Joining (or concatenating) arrays means combining two or more arrays into a single array.
NumPy provides multiple functions to join arrays:

concatenate()
stack()
hstack()
vstack()
dstack()
column_stack()


 1. Joining Using np.concatenate()


 

Output:

[1 2 3 4 5 6]

✔ Works for 1D, 2D, 3D arrays
✔ Must match dimension size except along joining axis.


 Concatenate 2D Arrays


 


 2. Joining Using stack()

Stack arrays along a new axis:


 

Output:

[[1 2 3]
[4 5 6]]


 Using axis


Output:

[[1 4]
[2 5]
[3 6]]


 3. hstack() — Join Horizontally (Row-wise)


 

Output:

[1 2 3 4 5 6]

 4. vstack() — Join Vertically (Column-wise)


 

Output:

[[1 2 3]
[4 5 6]]


 5. dstack() — Join Depth-wise (3D stacking)


 

Output:

[[[1 4]
[2 5]
[3 6]]
]

 6. column_stack() — Stack Column Format


 

Output:

[[1 4]
[2 5]
[3 6]]


🧠 Summary Table of Joining Methods

Function Type of Join Result
concatenate() Join along existing axis Flexible
stack() Create new dimension 2D/3D stacking
hstack() Horizontal stacking Side-by-side
vstack() Vertical stacking Top-bottom
dstack() Depth stacking 3D
column_stack() Column-wise stacking Matrix style

🎯 Example Practice


 

You may also like...