NumPy Tutorial · NumPy Tutorial

NumPy Array Join

Learn all about NumPy Array Join in this comprehensive tutorial.

5 min read intermediate
  • Joining means putting contents of two or more arrays in a single array.
  • Stacking is same as concatenation, the only difference is that stacking is done along a new axis.
  • NumPy provides a helper function: hstack() to stack along rows.
  • NumPy provides a helper function: vstack()  to stack along columns.
  • NumPy provides a helper function: dstack() to stack along height, which is the same as depth.

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.

python
python

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.

python

Stacking Along Rows

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

python

Stacking Along Columns

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

python

Stacking Along Height (depth)

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

python

Module quiz

2 questions
1

Which of the following is true about NumPy Array Join?

2

What is the most common pitfall when working with NumPy Array Join?

Answer all questions to submit.