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]]
Similar Reads
Combining a one and a two-dimensional NumPy Array Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer(), which provides this facility. Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0) Example 1
2 min read
Convert Python Nested Lists to Multidimensional NumPy Arrays Prerequisite: Python List, Numpy ndarray Both lists and NumPy arrays are inter-convertible. Since NumPy is a fast (High-performance) Python library for performing mathematical operations so it is preferred to work on NumPy arrays rather than nested lists. Method 1: Using numpy.array(). Approach : Im
2 min read
Evaluate Einstein's summation convention of two multidimensional NumPy arrays In Python, we can use the einsum() function of the NumPy package to compute Einstein's summation convention of two given multidimensional arrays. Syntax: numpy.einsum(subscripts, *operands, out=None)Â Parameters:Â subscripts : str Specifies the subscripts for summation as comma separated list of sub
3 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
Element-wise concatenation of two NumPy arrays of string In this article, we will discuss how to concatenate element-wise two arrays of string Example : Input : A = ['Akash', 'Ayush', 'Diksha', 'Radhika'] B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal'] Output : A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal'] We will be using the numpy.cha
2 min read