How To Concatenate NumPy Arrays

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 24, 2023
Spread the love

How to concatenate NumPy arrays in Python? You can use the numpy.concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array. In Python NumPy, we can join arrays by axes (vertical or horizontal), whereas in SQL we join tables based on keys.

You can pass a sequence of arrays that you want to join to the concatenate() function, along with the axis. If the axis is not explicitly passed, it is taken as 0. In this article, I will explain how to concatenate NumPy arrays (ndarray) with examples by using functions like concatenate(), stack(), hstack(), vstack(), dstack().

Note that in Python NumPyndarray is a multidimensional, homogeneous array of fixed-size items of the same type. You can create a ndarray object by using NumPy.array().

1. Quick Examples of NumPy Concatenate Arrays

If you are in a hurry, below are some quick examples of how to merge two NumPy arrays. Alternatively, you can also use NumPy.append() function to append arrays.


# Below are a quick examples

# Example 1: Use concatenate() to join two arrays
con = np.concatenate((arr, arr1))
print(con)

# Example 2: Use concatenate() with axis
con = np.concatenate((arr, arr1), axis=1)
print(con)

# Example 3: Use np.stack() function to Join Arrays
con = np.stack((arr, arr1), axis=1)
print(con)

# Example 4: Use np.hstack() function
con = np.hstack((arr, arr1))
print(con)

# Example 5: Use np.vstack() function 
con = np.vstack((arr, arr1))
print(con)

# Example 6: Use np.dstack() function to Stacking Along Height (depth)
con = np.dstack((arr, arr1))
print(con)

Let’s see each of these with examples.

2. Concatenate NumPy Arrays

Use numpy.concatenate() to merge the content of two or multiple arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray. Note that this method also takes axis as another argument, when not specified it defaults to 0.

Concatenation refers to putting the contents of two or more arrays in a single array. In Python NumPy, we can join arrays by axes (vertical or horizontal), whereas in SQL we join tables based on keys.


import numpy as np

#Create NumPy arrays
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])

# Use concatenate() to join two arrays
con = np.concatenate((arr, arr1))
print(con)

Yields below output. If you notice it just appends the elements from the second array to the first array and returns a new NumPy array.


[ 4  7 12  5  9 15]

3. Use numpy.concatenate() with axis=1

You can also concatenate two NumPy arrays column-wise by specifying axis=1. Now the resulting array is a wide matrix with more columns than rows. With axis=1, it returns an array of arrays (Nested array).


# Use concatenate() with axis
con = np.concatenate((arr, arr1), axis=1)
print(con)

Yields below output.


[[ 4  5]
 [ 7  9]
 [12 15]]

Now let’s see how to merge nested NumPy arrays.


# Create NumPy array
arr = np.array([[4, 6],[9, 13]])
arr1 = np.array([[8, 3],[12, 19]])

# Use np.concatenate() Function
con = np.concatenate((arr, arr1), axis=1)
print(con)

Yields below output.


[[ 4  6  8  3]
 [ 9 13 12 19]]

4. Use numpy.stack() Function to Join Arrays

Also, use numpy.stack() function to join a sequence of arrays along a new axis. You pass a sequence of arrays that you want to join to the numpy.stack() function along with the axis. If the axis is not explicitly passed it is taken as zero.


# Create NumPy arrays  
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])

# Use numpy.stack() Function to Join Arrays
con = np.stack((arr, arr1), axis=1)
print(con)

Yields below output. Since I used axis=1 it is concatenated on columns.


[[ 4  5]
 [ 7  9]
 [12 15]]

5. Use NumPy.hstack() Function

Similarly, you can also concatenate arrays horizontally in Python NumPy using hstack() function. The hstack() is used to stack the array horizontally.


# Create NumPy arrays 
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])
# Use NumPy.hstack() Functions
con = np.hstack((arr, arr1))
print(con)

Yields below output.


[ 4  7 12  5  9 15]

6. Use NumPy.vstack() Function

You can use numpy.vstack() to stack arrays in sequence vertically.


# Create numpy arrays 
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])

# Use numpy.hstack() function
con = np.vstack((arr, arr1))
print(con)

Yields below output.


[[ 4  7 12]
 [ 5  9 15]]

7. Use numpy.dstack() Function to Concatenate Arrays

Use numpy.dstack() to stack along with the height, which is the same as depth.


# Create NumPy arrays 
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])

# Use NumPy.dstack() function to Stacking Along Height (depth)
con = np.dstack((arr, arr1))
print(con)

Yields below output. This output is the same as using concatenate() with axis=1.


[[[ 4  5]
  [ 7  9]
  [12 15]]]

8. Conclusion

In this article, I have explained how to concatenate NumPy two or multiple arrays using the concatenate(), stack(), hstack(), vstack(), dstack() with examples. Concatenation refers to putting the contents of two or more arrays in a single array. In Python NumPy, we can join arrays by axes (vertical or horizontal), whereas in SQL we join tables based on keys.

Happy Learning!!

Related Articles

References

Leave a Reply

You are currently viewing How To Concatenate NumPy Arrays