# 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

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]]])```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.