# Combine two arrays into one in Python

**How to connect NumPy clusters(array) in Python? **

You can utilize the numpy.concatenate() capability to concat, union, or join a grouping of two or various clusters into a solitary NumPy exhibit. Link alludes to placing the items in at least two exhibits in a solitary cluster. In Python NumPy, we can join clusters by tomahawks (vertical or level), though in SQL we join tables in view of keys.

You can pass a grouping of clusters that you need to join to the link() capability, alongside the hub. On the off chance that the hub isn't expressly passed, it is taken as 0. In this article, we will clarify how for link NumPy clusters (ndarray) with models by utilizing capabilities like concatenate(), stack(), hstack(), vstack(), dstack().

Note: that in Python NumPy, ndarray is a multi-layered, homogeneous exhibit of fixed-size things of a similar sort. You can make a ndarray object by utilizing NumPy.array().

__1. using concatenate()__

To combine the contents of two or more arrays into one array, use numpy.concatenate(). This function concatenates NumPy arrays with a number of arguments and returns a Numpy array called ndarray. Keep in mind that this method also accepts an additional argument, axis, which defaults to 0 if left unspecified.

__Python Code:__

```
import numpy as np
#Create NumPy arrays
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])
# Use concatenate() to join two arrays
con = np.concatenate((arr, arr1))
print(con)
```

__Output:__

__2. using numpy.stack()__

To join a series of arrays along a new axis, use numpy.stack(). The axis and a list of arrays you want to join are passed to the numpy.stack() function. The axis is assumed to be zero if it is not explicitly passed.

__Python Code:__

```
import numpy as np
#Create NumPy arrays
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])
con = np.stack((arr, arr1), axis=1)
print(con)
```

__Output:__

__3. Use NumPy.hstack() Function__

This function does not work with the axis. It extends the first array by the second array

Horizontally.

As it extends horizontally, both the arrays should have the same number of rows else

Value Error will be returned.

__Example:__

__Python Code:__

```
import numpy as np
#Create NumPy arrays
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])
con = np.hstack((arr, arr1))
print(con)
```

__Output:__

__4. NumPy.vstack() Function__

This function does not work with axis. It extends the first array by the second array

Vertically.

__Python Code:__

```
import numpy as np
#Create NumPy arrays
arr = np.array([4, 7, 12])
arr1 = np.array([5, 9, 15])
con = np.vstack((arr, arr1))
print(con)
```

__Output:__

__5. Use numpy.dstack() Function__

The dstack() is used to stack arrays in sequence depth-wise (along the third axis).

I hope you like this article.

Thanks

Amandeep Singh