Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to Concatenate two 2-dimensional NumPy Arrays?

How to Concatenate two 2-dimensional NumPy Arrays?

Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it:

# import numpy package
import numpy as np

Then two 2D arrays have to be created to perform the operations, by using arrange() and reshape() functions. Using NumPy, we can perform concatenation of multiple 2D arrays in various ways and methods.

Method 1: Using concatenate() function

We can perform the concatenation operation using the concatenate() function. With this function, arrays are concatenated either row-wise or column-wise, given that they have equal rows or columns respectively. Column-wise concatenation can be done by equating axis to 1 as an argument in the function.

Example:

Python




# Program to concatenate two 2D arrays column-wise
# import numpy
import numpy as np
 
# Creating two 2D arrays
arr1 = np.arange(1,10).reshape(3,3)
arr2 = np.arange(10,19).reshape(3,3)
arr1
arr2
 
# Concatenating operation
# axis = 1 implies that it is being done column-wise
np.concatenate((arr1,arr2),axis=1)


Output:

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
       
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
       
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

In the same way, row-wise concatenation can be done by equating axis to 0.

Example:

Python




# Program to concatenate two 2D arrays row-wise
import numpy as np
 
# Creating two 2D arrays
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
# axis = 0 implies that it is being done row-wise
np.concatenate((arr1, arr2), axis=0)


Output:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

Method 2: Using stack() functions:

The stack() function can be used in the same way as the concatenate() function where the axis is equated to one. The arrays are stacked one over the other by using this.

Example:

Python




# Program to concatenate two 2D arrays row-wise
import numpy as np
 
 
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
# axis = 1 implies that it is being
# done row-wise
np.stack((arr1, arr2), axis=1)


Output:
 

array([[[ 1,  2,  3],
        [10, 11, 12]],

       [[ 4,  5,  6],
        [13, 14, 15]],

       [[ 7,  8,  9],
        [16, 17, 18]]])

Or by equating axis to 2 the concatenation is done along with the height as shown below.

Example:

Python3




# Program to concatenate two 2D arrays along
# the height
import numpy as np
 
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
# axis = 2 implies that it is being done
# along the height
np.stack((arr1, arr2), axis=2)


Output:

array([[[ 1, 10],
        [ 2, 11],
        [ 3, 12]],

       [[ 4, 13],
        [ 5, 14],
        [ 6, 15]],

       [[ 7, 16],
        [ 8, 17],
        [ 9, 18]]])

Method 3: Using hstack() function

The hstack() function stacks the array horizontally i.e. along a column.

Example:

Python




# Program to concatenate two 2D arrays
# horizontally
import numpy as np
 
 
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
arr = np.hstack((arr1, arr2))


Output:

array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

Method 4: Using vstack() function

The vstack() function stacks arrays vertically i.e. along a row.

Example:

Python




# Program to concatenate two 2D arrays
# vertically
import numpy as np
 
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
arr = np.vstack((arr1, arr2))


Output:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

Method 5: Using dstack() function

In the dstack() function, d stands for depth and the concatenations occur along with the height as shown below:

Example:

Python




# Program to concatenate two 2D arrays
# along the height
import numpy as np
 
arr1 = np.arange(1, 10).reshape(3, 3)
arr2 = np.arange(10, 19).reshape(3, 3)
 
# Concatenating operation
arr = np.dstack((arr1, arr2))


Output:

array([[[ 1, 10],
        [ 2, 11],
        [ 3, 12]],

       [[ 4, 13],
        [ 5, 14],
        [ 6, 15]],

       [[ 7, 16],
        [ 8, 17],
        [ 9, 18]]])

Method 6: Using column_stack() function

column_stack() function stacks the array horizontally i.e. along a column,  it is usually used to concatenate id arrays into 2d arrays by joining them horizontally.

Python3




import numpy
 
 
array1 = numpy.array([[1, 2, 3, 4, 5],[20,30,40,50,60]])
array2 = numpy.array([[6, 7, 8, 9, 10],[9,8,7,6,5]])
 
# Stack arrays horizontally.
array1 = numpy.column_stack([array1, array2])
print(array1)


Output:

[[ 1  2  3  4  5  6  7  8  9 10]

 [20 30 40 50 60  9  8  7  6  5]]

RELATED ARTICLES

Most Popular

Recent Comments