All Courses

Join array in python

Shashank Shanu

2 years ago

Join Array In Python
Table of Content
  • Joining NumPy Arrays using concatenate () function
  • Joining Arrays using Stack Functions in python.
  • Stacking Along Rows: hstack()
  • Stacking Along Columns
           As you observed many times, we need to join two different arrays while performing any machine learning or deep learning tasks. In this article, I will try to show you how we can join two arrays using different available functions in python.
So, let’s start

Joining NumPy Arrays using concatenate () function

       In SQL, we join two different tables based on a primary key, whereas in NumPy we join arrays based on their axes.
Let’s take an example:
# import library
import numpy as np
# create array
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
joined_arr = np.concatenate((array1, array2))
print(joined_arr)
Output: When we execute the above code it will give us the result as follows:
[ 1  2  3  4  5  6  7  8  9 10]
In the above example, we can see that we used. concatenate () function of python, which takes a sequence of arrays that we want to join along with the axis. If axis is not explicitly given as an argument, it by default takes 0.

Joining Arrays using Stack Functions in python.

          There is one method to join array in python called Stacking. It is same as concatenation but 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. This is known as stacking.
Let’s take an example:
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
stacked_array = np.stack((array1, array2), axis=1)
print(stacked_array)
Output: When we execute the above code it will give us the result as follows:
[[ 1  6]
 [ 2  7]
 [ 3  8]
 [ 4  9]
 [ 5 10]]
In the above example, we used the stack () function present in python. We have to pass a sequence of arrays that we want to join along with the axis. If the axis is not explicitly passed it is taken as 0.

Stacking Along Rows: hstack()

       NumPy packages provide us with a helper function: hstack() which is used when we want to stack two arrays along rows.
Example:
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
hstacked_array = np.hstack((array1, array2))
print(hstacked_array)
Output: When we execute the above code it will give us the result as follows:
[ 1  2  3  4  5  6  7  8  9 10]
Note: While applying hstack function we do not provide any axis. As it horizontally stacks python understand itself.

Stacking Along Columns

         NumPy package also provides us with a helper function known as: vstack() function which is used when we want to stack two arrays along columns.
Example:
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([6, 7, 8, 9, 10])
vstacked_array = np.vstack((array1, array2))
print(vstacked_array)
Output: When we execute the above code it will give us the result as follows:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
Note: While applying vstack function we do not to provide any axis. As it vertically stacks python understand itself.
I hope after you enjoyed reading this article and finally, you came to know about Join array in python
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Programming…

Submit Review