Python NumPy – Concatenate() Function

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 27, 2023
  • Reading time:13 mins read

In NumPy, the concatenate() function is used to join two or more arrays along an existing axis. To join a sequence of arrays along an axis (row/column). This function is used to join two or more arrays of the same shape along a specified axis. In this article, I will explain NumPy concatenate() function syntax and usage with examples.

1. Quick Examples of NumPy concatenate() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy concatenate() function.


# Quick examples of numpy concatenate()

# Example 1: Use np.concatenate() function
con = np.concatenate((arr, arr1))

# Example 2: Use Joining the two arrays along axis 0
con = np.concatenate((arr, arr1), axis = 0)

# Example 3: Use Joining the two arrays along axis 1
con = np.concatenate((arr, arr1), axis = 1)

# Example 4: Use Joining the two arrays along axis=None
con = np.concatenate((arr, arr1), axis = None)

2. NumPy concatenate() Syntax

Following is the syntax of the concatenate() function.


# numpy.concatenate() syntax
numpy.concatenate((arr, arr1, ...), axis=0, out=None)
  • arr,arr1 : a sequence of array_like. The arrays must have the same shape, except in the dimension corresponding to the axis (the first, by default).
  • axis : Axis along which arrays will be joined. The default is 0, indicating that the arrays will be concatenated along the first axis. If you specify a different axis, the arrays must have the same shape along that axis.
  • out : If provided, the result will be placed into this array. It must be of the appropriate shape and dtype.

2.2 Return value

It returns the concatenated array.

3. Use numpy.concatenate() Method

Use numpy.concatenate() to put the content of two or more arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray. If the axis is not passed; it is taken as 0.

You can use the np.concatenate() function to concatenate two 2D arrays along the default axis (axis=0). For instance, arr and arr1 are concatenated along the default axis (axis=0), resulting in a new array where the rows of arr1 are appended to the rows of arr.


# Import numpy
import numpy as np
 
# Create 2D input array
arr = np.array([[4, 6], [9, 13]])
arr1 = np.array([[8, 3], [12, 19]])

# Use np.concatenate() function
con = np.concatenate((arr, arr1))
print("Concatenate two arrays:\n",con)

Yields below output.

NumPy concatenate() function

4. Use Joining the Two Arrays along Axis = 0

If you want to join the two arrays along axis 0 using the np.concatenate() function, you can use the default axis value (axis=0). In this case, arr and arr1 are concatenated along the default axis (axis=0), resulting in a new array where the rows of arr1 are appended to the rows of arr.

You can join two NumPy arrays row-wise by specifying axis=0. Now the resulting array is a wide matrix with more columns than rows; follow the below example, 4 rows, and 2 columns.


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

# Use Joining the two arrays along axis 0
con = np.concatenate((arr, arr1), axis = 0)
print("Concatenate along axis 0:\n", con)

Yields the same output as above.

5. Use Joining the Two Arrays along Axis = 1

If you want to join the two arrays along axis 1 using the np.concatenate() function, you can specify axis=1. In this case, arr and arr1 are concatenated along axis 1, resulting in a new array where the columns of arr1 are appended to the columns of arr.

To join two NumPy arrays column-wise by specifying axis=1. Now the resulting array is a wide matrix with more columns than rows.


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

# Use Joining the two arrays along axis 1
con = np.concatenate((arr, arr1), axis = 1)
print("Concatenate along axis 1:\n", con)

Yields below output.


# Output:
Concatenate along axis 1:
 [[ 4  6  8  3]
 [ 9 13 12 19]]

6. Use numpy.concatenate() with Axis=None

To using the np.concatenate function to concatenate the two arrays arr and arr1 along axis=None. When axis=None, the arrays are flattened before concatenation. In this output, the arrays arr and arr1 are flattened before concatenation, resulting in a 1D array containing all the elements from both arrays.


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

# Use joining the two arrays along axis=None
con = np.concatenate((arr, arr1), axis = None)
print("Concatenate along axis None:\n", con)

Yields below output.


# Output:
Concatenate along axis None:
 [ 4  6  9 13  8  3 12 19]

Frequently Asked Questions

What does the numpy.concatenate() function do?

The numpy.concatenate() function is used to concatenate (join) two or more arrays along a specified axis.

Can I use axis=None with numpy.concatenate()?

You cannot use axis=None with numpy.concatenate(). The axis parameter in the numpy.concatenate() function must be an integer or a tuple of integers, specifying the axis along which the arrays will be concatenated.

How can I concatenate arrays along the rows (axis=0)?

To concatenate arrays along the rows (axis=0), you can use the numpy.concatenate() function with the default value for the axis parameter (which is 0).

What happens if the shapes of arrays are not compatible for concatenation?

The arrays must have the same shape along the specified axis (except for the dimension along that axis). If shapes are not compatible, a ValueError will be raised.

Is there an alternative to numpy.concatenate() for concatenating along a new axis?

You can use numpy.stack() for concatenating along a new axis. For vertical stacking, numpy.vstack() and for horizontal stacking, numpy.hstack() can also be used.

Can I concatenate more than two arrays?

You can concatenate more than two arrays by providing them as a tuple in the numpy.concatenate() function, like this: numpy.concatenate((arr1, arr2, arr3), axis=0).

Conclusion

In this article, I have explained how to use the NumPy concatenate() function to join two or more arrays into a single Numpy array with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply