NumPy Joining Array

NumPy Tutorial

 NumPy Joining Array

NumPy Joining Array 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

FunctionType of JoinResult
concatenate()Join along existing axisFlexible
stack()Create new dimension2D/3D stacking
hstack()Horizontal stackingSide-by-side
vstack()Vertical stackingTop-bottom
dstack()Depth stacking3D
column_stack()Column-wise stackingMatrix style

 Example Practice


 

You may also like...