How to Concatenate two 2-dimensional NumPy Arrays? - GeeksforGeeks

09 Aug, 2021
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.
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 numpyimport numpy as np # Creating two 2D arraysarr1 = np.arange(1,10).reshape(3,3)arr2 = np.arange(10,19).reshape(3,3)arr1arr2 # Concatenating operation# axis = 1 implies that it is being done column-wisenp.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-wiseimport numpy as np # Creating two 2D arraysarr1 = 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-wisenp.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]])
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-wiseimport 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-wisenp.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 heightimport 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 heightnp.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]]])
The hstack() function stacks the array horizontally i.e. along a column.
Example:
Python
# Program to concatenate two 2D arrays# horizontallyimport numpy as np  arr1 = np.arange(1, 10).reshape(3, 3)arr2 = np.arange(10, 19).reshape(3, 3) # Concatenating operationarr = np.hstack((arr1, arr2))
Output:
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])
The vstack() function stacks arrays vertically i.e. along a row.
Example:
Python
# Program to concatenate two 2D arrays# verticallyimport numpy as np arr1 = np.arange(1, 10).reshape(3, 3)arr2 = np.arange(10, 19).reshape(3, 3) # Concatenating operationarr = np.vstack((arr1, arr2))
Output:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
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 heightimport numpy as np arr1 = np.arange(1, 10).reshape(3, 3)arr2 = np.arange(10, 19).reshape(3, 3) # Concatenating operationarr = 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]]
pulamolusaimohan
Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python