# Array Join In NumPy

In this article, we will explore Numpy Array Join in detail and examine their use cases with examples.

Numpy is a powerful Python library used for scientific computing and data analysis.

It provides a variety of functions and tools for manipulating arrays and matrices. One of the essential operations in array manipulation is joining arrays.

Numpy offers several methods for joining arrays, including concatenation, stacking, and appending.

## Numpy Array Join

With Numpy array join, we combine the contents of two or more arrays into one.

NumPy joins arrays by axes, while SQL joins tables by keys.

Concatenation is the process of combining two or more arrays along a particular axis.

Numpy provides the concatenate() function to perform this operation. The function takes two or more arrays as arguments and concatenates them along the specified axis.

we provide a sequence of arrays to be joined, along with the axis. Axis is assumed to be 0 if it is not specifically mentioned.

Merge two arrays by applying the concatenate method:

#### Example:

import numpy as npy car1_arr = npy.array(["Porsche", "Tesla", "Kia", "Peugeot"]) car2_arr = npy.array(["Honda", "Jaguar", "Mazda", "Volvo"]) mrx_arr = npy.concatenate((car1_arr, car2_arr)) print(mrx_arr) Utilize the concatenate method to two arrays to combine them:

#### Example:

import numpy as npy palindrome1_arr = npy.array(["wow", "peep", "rotator", "noon"]) palindrome2_arr = npy.array(["deed", "Madam ", "Civic", "Level"]) mrx_arr = npy.concatenate((palindrome1_arr, palindrome2_arr)) print(mrx_arr)

In the rows, connect two two-dimensional arrays (with axis 1):

#### Example:

import numpy as npy prime1_arr = npy.array([[2, 3], [5, 7]]) prime2_arr = npy.array([[11, 13], [17, 19]]) mrx_arr = npy.concatenate((prime1_arr, prime2_arr), axis=1) print(mrx_arr)

Combine the two two-dimensional arrays even_arr and odd_arr to the rows (with axis one):

#### Example:

import numpy as npy even_arr = npy.array([[0, 2, 4], [6, 8, 10]]) odd_arr = npy.array([[1, 3, 5], [7, 9, 11]]) mrx_arr = npy.concatenate((even_arr, odd_arr), axis=1) print(mrx_arr)
Note that the arrays must have the same shape along the axis being concatenated. If the arrays have different shapes, Numpy will raise a ValueError.

## Join Array with Stack()

Stacking is similar to concatenation, but it creates a new axis in the resulting array.

Numpy provides the stack() function to perform this operation. The function takes two or more arrays as arguments and stacks them along the specified axis.

When Numpy array joins two 1-D arrays, we can merge them along the second axis, producing stacking.

In addition to the axis, we provide a sequence of arrays to the stack() method. In the absence of axis, 0 is assumed.

Provide the order to arrays and combine it to the stack method via axis:

#### Example:

import numpy as npy trigonometry1_arr = npy.array(["Sin θ", "Cos θ", "Tan θ"]) trigonometry2_arr = npy.array(["Cot θ", "Sec θ", "Cosec θ"]) mrx_arr = npy.stack((trigonometry1_arr, trigonometry2_arr), axis=1) print(mrx_arr)

By utilizing axis, assign the sequence to arrays and connect it with the stack method:

#### Example:

import numpy as npy stringMethods1_arr = npy.array(["capitalize()", "casefold()", "center()", "count()", "encode()"]) stringMethods2_arr = npy.array(["find()", "format()", "index()", "isalpha()", "isdigit()"]) mrx_arr = npy.stack((stringMethods1_arr, stringMethods2_arr), axis=1) print(mrx_arr)

## hstack() Rows

NumPy offers an assistive function for stacking along rows: hstack().

Stack the data of two arrays in one row:

#### Example:

import numpy as npy dicMethod1_arr = npy.array(["clear()", "copy()", "fromkeys()", "get()"]) dicMethod2_arr = npy.array(["items()", "keys()", "values()", "update()"]) mrx_arr = npy.hstack((dicMethod1_arr, dicMethod2_arr)) print(mrx_arr)

Put two arrays frameworks1_arr and frameworks2_arr in a row and arrange their contents:

#### Example:

import numpy as npy frameworks1_arr = npy.array(["Django", "CherryPy", "Pyramid", "Grok", "TurboGears"]) frameworks2_arr = npy.array(["Web2Py", "Flask", "Bottle", "Tornado"]) mrx_arr = npy.hstack((frameworks1_arr, frameworks2_arr)) print(mrx_arr)

## vStack() Columns

With NumPy, you can create columns of stacks by calling vstack().

Arrange the data of two arrays in a column:

#### Example:

import numpy as npy keywords1_arr = npy.array(["and", "as", "assert", "break", "class"]) keywords2_arr = npy.array(["continue", "def", "del", "elif", "else"]) mrx_arr = npy.vstack((keywords1_arr, keywords2_arr)) print(mrx_arr)

By calling vstack() you can align the items of two arrays physicsConst1_arr and physicsConst2_arr in a column:

#### Example:

import numpy as npy physicsConst1_arr = npy.array(["Newtonian constant of gravitation", "speed of light in vacuum", "Planck constant", "vacuum magnetic permeability"]) physicsConst2_arr = npy.array(["vacuum electric permittivity", "Coulomb constant", "Boltzmann constant", "Stefan–Boltzmann constant"]) mrx_arr = npy.vstack((physicsConst1_arr, physicsConst2_arr)) print(mrx_arr)

## dStack() Height

The NumPy stacking function dstack() stacks according to height, which is identical to depth.

Sequence the arrays according to their height by calling the dstack() function:

#### Example:

import numpy as npy fibonacci_arr = npy.array([ 0, 1, 1, 2, 3, 5, 8]) factorial_arr = npy.array([0, 1, 2, 6, 24, 120, 720]) mrx_arr = npy.dstack((fibonacci_arr, factorial_arr)) print(mrx_arr)

By invoking the dstack() function, you can order the arrays based on their height:

#### Example:

import numpy as npy consonants1_arr = npy.array(["B", "C", "D", "F", "G", "H", "J", "K"]) consonants2_arr = npy.array(["L", "M", "N", "P", "Q", "R", "S", "T"]) mrx_arr = npy.dstack((consonants1_arr, consonants2_arr)) print(mrx_arr)

In conclusion, NumPy array joining is an essential operation in array manipulation, and Numpy provides several methods for performing this task.

By understanding the differences between all methods, we can choose the appropriate method for our specific use case.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0 