Introduction to Python NumPy Joining Array

In This Article, You Will Learn About Python NumPy Joining

Python Numpy Joining – Before moving ahead, let’s know a little bit about Python NumPy Iterating

Joining NumPy Arrays

A join is when you combine the contents of multiple arrays into one.

SQL joins tables based upon a key; NumPy joins arrays by axes.

A sequence of arrays to be joined to the concatenate() function is passed along with the axis. If axis isn’t explicitly passed, it will be taken as 0.

Example – Joining two arrays.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.concatenate((Array_1, Array_2))

print(Array)
Output - 

[11 12 13 14 15 16]

As a result, it returned a joint array.

Example – Joining two 2-D arrays along rows (axis=1).

import numpy as np

Array_1 = np.array([[11, 12], [13, 14]])
Array_2 = np.array([[15, 16], [17, 18]])
Array = np.concatenate(( Array_1 , Array_2), axis=1)

print(Array)
Output - 

[[11 12 15 16]
[13 14 17 18]]

As can be seen, it returned concatenated arrays along axis 1.

Joining Arrays Using Stack Functions

Stacking is same as concatenation, the only difference is that stacking is done along a new axis.

We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.

We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0.

Stacking Along Rows

NumPy provides a helper function: hstack() to stack along rows.

Example – Joining two arrays.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.hstack((Array_1, Array_2))

print(Array)
Output - 

[11 12 13 14 15 16]

All things considered, it returned concatenated arrays along axis 1.

Stacking Along Columns

NumPy provides a helper function: vstack()  to stack along columns.

Example – Joining two arrays along columns.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.vstack((Array_1, Array_2))

print(Array)
Output - 

[[1 2 3]
[4 5 6]]

As has been noted, it returned concatenated arrays along columns.

Stacking along Height (depth).

Example – Joining two arrays along depth.

NumPy provides a helper function: dstack() to stack along height, which is the same as depth.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.dstack((Array_1, Array_2))

print(Array)
Output - 

[[[11 14]
[12 15]
[13 16]]]

Henceforth, it returned concatenated arrays along columns.

Joining NumPy Arrays

A join is when you combine the contents of multiple arrays into one.

SQL joins tables based upon a key; NumPy joins arrays by axes.

A sequence of arrays to be joined to the concatenate() function is passed along with the axis. If axis isn’t explicitly passed, it will be taken as 0.

Example – Joining two arrays.

import numpy as np

Array_1 = np.array([1, 2, 3])
Array_2 = np.array([4, 5, 6])
Array = np.concatenate((Array_1, Array_2))

print(Array)
Output - 

[1 2 3 4 5 6]

To summarize, it returned concatenate arrays.

Example – Joining two 2-D arrays along rows (axis=1).

import numpy as np

Array_1 = np.array([[11, 12], [13, 14]])
Array_2 = np.array([[15, 16], [17, 18]])
Array = np.concatenate((Array_1, Array_2), axis=1)

print(Array)
Output - 

[[11 12 15 16]
[13 14 17 18]]

As a result, it returned concatenation of two 2-D arrays along rows (axis=1).

Joining Arrays Using Stack Functions

Stacking is same as concatenation, the only difference is that stacking is done along a new axis.

We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking.

We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0.

Stacking Along Rows

Example – Joining two arrays along rows.

NumPy provides a helper function: hstack() to stack along rows.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.hstack((Array_1, Array_2))

print(Array)
Output - 

[11 12 13 14 15 16]

Overall, it returned concatenation of two 1-D arrays along rows (axis=1).

Stacking Along Columns

NumPy provides a helper function: vstack()  to stack along columns.

Example – Adding two arrays along columns.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.vstack((Array_1, Array_2))

print(Array)
python-numpy-joining

As a result, it returned addition of two arrays along columns.

Stacking along Height (depth).

NumPy provides a helper function: dstack() to stack along height, which is the same as depth.

Example – Adding two arrays along height.

Example – Adding two arrays along height.

import numpy as np

Array_1 = np.array([11, 12, 13])
Array_2 = np.array([14, 15, 16])
Array = np.dstack((Array_1, Array_2))

print(Array)
Output - 

[11 12 13 14 15 16]

As shown above , it returned Addition of two arrays along columns.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

1 thought on “Introduction to Python NumPy Joining Array”

Leave a Comment

Stay in the loop

codingstreets