How to Concatenate NumPy Arrays

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 10, 2023

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.


# Quick examples of numpy concatenate arrays

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

# Example 2: Concatenating arrays along axis 1 (horizontally)
result = np.concatenate((array1, array2), axis=1)

# Example 3: Concatenating arrays along axis= None
result = np.concatenate((array1, array2), axis=None)

# Example 4: Using numpy.stack() function
# To join arrays along a new axis 
result = np.stack((array1, array2), axis=0)

# Example 5: Use numpy.stack() function 
# To join arrays
result = np.stack((array1, array2), axis=1)

# Example 6: Using numpy.hstack() function
# Concatenate arrays horizontally
result = np.hstack((array1, array2))

# Example 7: Using numpy.vstack() function
# Concatenate arrays vertically
result = np.vstack((array1, array2))

# Example 8: Using numpy.dstack() function
# Concatenate 2D arrays along the depth axis
result = np.dstack((array1, array2))

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 the 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
array1 = np.array([2, 4, 8])
print("First numpy arrays:\n", array1)
array2 = np.array([5, 9, 7])
print("Second numpy arrays:\n", array2)

# Use concatenate() to join two arrays
result = np.concatenate((array1, array2))
print("After concatenating numpy arrays:\n", result)

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.

numpy concatenate arrays

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

To numpy.concatenate() function is used to join two or more arrays along a specified axis. When axis=1, it means that the arrays will be joined horizontally, i.e., along columns.

In the below example, numpy.concatenate() is used to concatenate array1 and array2 along axis=1. The elements from the second array (array2) are appended as columns to the right of the elements from the first array (array1).


import numpy as np

# Creating two 2D arrays
array1 = np.array([[1, 2, 3],
                  [4, 5, 6]])
print("First numpy arrays:\n", array1)
array2 = np.array([[7, 8, 9],
                  [10, 11, 12]])
print("Second numpy arrays:\n", array2)

# Concatenating arrays along axis 1 (horizontally)
result = np.concatenate((array1, array2), axis=1)
print("After concatenating the array along axis 1:\n", result)

Yields below output.

numpy concatenate arrays

Similarly, you use axis=None in np.concatenate(), it flattens both input arrays and concatenates them into a 1D array.

In the below example, axis=None causes both array1 and array2 to be flattened into 1D arrays, and then these 1D arrays are concatenated. All the elements from array1 and array2 are combined into a single 1D array due to the axis=None parameter.


# Concatenating arrays along axis= None
result = np.concatenate((array1, array2), axis=None)
print("After concatenated array along axis None:\n", result)

# Output:
# After concatenated array along axis None:
#  [ 1  2  3  4  5  6  7  8  9 10 11 12]

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

The numpy.stack() function is used to join arrays along a new axis. For instance, numpy.stack() is used to join array1 and array2 along a new axis (axis=0, which means along rows).

The elements array1 form the first row, and the elements array2 form the second row of the resulting 2D array.


import numpy as np

# Creating two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Using numpy.stack() function
# To join arrays along a new axis 
result = np.stack((array1, array2), axis=0)
print("After concatenated array:\n", result)

# Output:
# After concatenated array:
#  [[1 2 3]
#  [4 5 6]]

You can also use axis=1 it to stack the arrays along columns. 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.


# Use numpy.stack() function 
# To join arrays
result = np.stack((array1, array2), axis=1)
print("After concatenated array:\n", result)

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


# Output:
After concatenated array:
  [[1 4]
  [2 5]
  [3 6]]

5. Use numpy.hstack() Function

The numpy.hstack() function is used to stack the sequence of input arrays horizontally (i.e., along columns). For instance, numpy.hstack() is used to concatenate array1 and array2 horizontally. The elements from array1 and array2 are concatenated into a single 1D array horizontally.


import numpy as np

# Creating two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Using numpy.hstack() function
# Concatenate arrays horizontally
result = np.hstack((array1, array2))
print("After concatenated arrays horizontally:\n", result)

Yields below output.


# Output:
After concatenated arrays horizontally:
 [1 2 3 4 5 6]

6. Use numpy.vstack() Function

You can use numpy.vstack() function is used to stack the sequence of input arrays vertically (i.e., along rows).

In the below example, numpy.vstack() is used to concatenate array1 and array2 vertically. The elements array1 form the first row, and the elements array2 form the second row of the resulting 2D array.


import numpy as np

# Creating two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Using numpy.vstack() function
# Concatenate arrays vertically
result = np.vstack((array1, array2))
print("After concatenated arrays vertically:\n", result)

Yields below output.


# Output:
After concatenated arrays vertically:
 [[1 2 3]
 [4 5 6]]

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

Use numpy.dstack() to stack along with the height, which is the same as the depth. This function is used to stack arrays along the third axis (depth).

In the below example, numpy.dstack() is used to concatenate array1 and array2 along the depth axis. In the resulting 3D array, the elements from array1 and array2 are stacked along the third axis, creating a new dimension for the depth.


import numpy as np

# Creating two 2D arrays
array1 = np.array([[1, 2, 3],
                  [4, 5, 6]])                   
array2 = np.array([[7, 8, 9],
                  [10, 11, 12]])

# Using numpy.dstack() function
# Concatenate 2D arrays along the depth axis
result = np.dstack((array1, array2))
print("After concatenated arrays along depth:\n", result)

Yields below output.


# Output:
After concatenated arrays along depth:
 [[[ 1  7]
  [ 2  8]
  [ 3  9]]

 [[ 4 10]
  [ 5 11]
  [ 6 12]]]

Frequently Asked Questions of NumPy Concatenate Arrays

What is array concatenation in NumPy?

The array concatenation in NumPy refers to the process of combining two or more arrays along a particular axis to create a new array. This operation allows you to join arrays either vertically, horizontally, or along a specified axis, effectively extending the size or dimensions of the arrays.

How do I concatenate arrays vertically (along rows) in NumPy?

You can concatenate arrays vertically (along rows) in NumPy using the np.vstack() function or the np.concatenate() function with the axis parameter set to 0.

How do I concatenate arrays horizontally (along columns) in NumPy?

You can concatenate arrays horizontally (along columns) in NumPy using the np.hstack() function or the np.concatenate() function with the axis parameter set to 1.

Can I concatenate arrays along multiple axes simultaneously?

Yes, you can concatenate arrays along multiple axes by using np.concatenate() multiple times, specifying different axes for concatenation each time. Alternatively, you can use functions like np.vstack() and np.hstack() multiple times to concatenate arrays along different axes.

Can I concatenate arrays with different shapes?

You can concatenate arrays with different shapes in NumPy, but there are certain rules that you need to follow to ensure proper concatenation. When concatenating arrays along a particular axis, the dimensions of the arrays along that axis should be the same, except for the dimension along the concatenation axis. For instance, if you want to concatenate arrays along axis=0 (vertical concatenation), the arrays you want to concatenate should have the same number of columns. If you want to concatenate along axis=1 (horizontal concatenation), the arrays should have the same number of rows.

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!!

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing How to Concatenate NumPy Arrays