NumPy Array Join

In NumPy, joining means combining two or more arrays into a single array. NumPy provides different methods to join arrays, such as concatenate(), stack(), hstack(), and vstack(). These methods are useful when combining student records, sales data, and other datasets.

1. What is Array Joining in NumPy?

Array joining combines multiple arrays into one array.

The concatenate() function is commonly used to join arrays along an existing axis.

Example:

import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])

result = np.concatenate((arr1, arr2))

print(result)

Output:

[10 20 30 40 50 60]

Explanation:

  • arr1 contains the first three elements.
  • arr2 contains the next three elements.
  • np.concatenate() combines both arrays into one array.

2. Joining Two 2-D Arrays

You can also join two-dimensional arrays using concatenate(). The axis parameter determines the direction of joining.

Example:

import numpy as np

arr1 = np.array([
    [10, 20],
    [30, 40]
])

arr2 = np.array([
    [50, 60],
    [70, 80]
])

result = np.concatenate((arr1, arr2), axis=0)

print(result)

Output:

[[10 20]
 [30 40]
 [50 60]
 [70 80]]

Explanation:

  • axis=0 joins arrays vertically (row-wise).
  • The rows of the second array are added below the first array.
  • Both arrays must have the same number of columns.

3. Joining Arrays Horizontally Using axis=1

You can use axis=1 to join two 2-D arrays horizontally (column-wise).

Example:

import numpy as np

arr1 = np.array([
    [10, 20],
    [30, 40]
])

arr2 = np.array([
    [50, 60],
    [70, 80]
])

result = np.concatenate((arr1, arr2), axis=1)

print(result)

Output:

[[10 20 50 60]
 [30 40 70 80]]

Explanation:

  • axis=1 joins arrays column-wise.
  • The columns of the second array are added beside the first array.
  • Both arrays must have the same number of rows.

4. Joining Arrays Using stack()

The stack() function combines arrays along a new axis. Unlike concatenate(), it creates an additional dimension.

Example:

import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])

result = np.stack((arr1, arr2))

print(result)

Output:

[[10 20 30]
 [40 50 60]]

Explanation:

  • np.stack() combines both arrays along a new axis.
  • The resulting array has 2 dimensions.
  • Each original array becomes one row.

5. Joining Arrays Using hstack()

The hstack() function joins arrays horizontally. For 1-D arrays, it combines their elements into one longer array.

Example:

import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])

result = np.hstack((arr1, arr2))

print(result)

Output:

[10 20 30 40 50 60]

Explanation:

  • hstack() joins arrays horizontally.
  • For 1-D arrays, it places the elements of the second array after the first.
  • For 2-D arrays, it joins columns side by side.

6. Joining Arrays Using vstack()

The vstack() function joins arrays vertically. For 1-D arrays, it places each array into a separate row.

Example:

import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])

result = np.vstack((arr1, arr2))

print(result)

Output:

[[10 20 30]
 [40 50 60]]

Explanation:

  • vstack() joins arrays vertically.
  • Each 1-D array becomes a separate row.
  • The resulting array is two-dimensional.

7. Joining Arrays Using dstack()

The dstack() function joins arrays along the third axis (depth). For two 1-D arrays, it creates a 2-D array with an additional third dimension.

Example:

import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([40, 50, 60])

result = np.dstack((arr1, arr2))

print(result)

Output:

[[[10 40]]
 [[20 50]]
 [[30 60]]]

Explanation:

  • dstack() joins arrays along the third axis.
  • Each pair of corresponding elements is placed together.
  • The resulting array has three dimensions.

8. Difference Between NumPy Joining Functions

NumPy provides different joining functions depending on how you want to combine arrays.

FunctionPurpose
concatenate()Joins arrays along an existing axis
stack()Joins arrays along a new axis
hstack()Joins arrays horizontally
vstack()Joins arrays vertically
dstack()Joins arrays along the third axis

Important: The arrays must have compatible shapes. For example, when joining 2-D arrays along axis=0, they must have the same number of columns.

Student Task

A teacher stores marks for two groups of students.

import numpy as np

group1 = np.array([75, 80, 85])
group2 = np.array([90, 95, 88])

Complete these tasks:

  1. Join both arrays using concatenate().
  2. Join both arrays using stack().
  3. Join both arrays using hstack().
  4. Join both arrays using vstack().
  5. Join both arrays using dstack().
  6. Print the shape of each resulting array.

Final Student Challenge: Combine Company Sales Records

A company stores monthly sales records for two departments. Each department has sales figures for three employees across three months.

import numpy as np

department1 = np.array([
    [10000, 12000, 15000],
    [18000, 20000, 22000],
    [14000, 16000, 19000]
])

department2 = np.array([
    [11000, 13000, 16000],
    [17000, 21000, 23000],
    [15000, 18000, 20000]
])

# Join departments vertically
vertical_sales = np.concatenate(
    (department1, department2), axis=0
)

# Join departments horizontally
horizontal_sales = np.concatenate(
    (department1, department2), axis=1
)

print("Vertical Sales:")
print(vertical_sales)

print("\nHorizontal Sales:")
print(horizontal_sales)

Expected Output:

Vertical Sales:
[[10000 12000 15000]
 [18000 20000 22000]
 [14000 16000 19000]
 [11000 13000 16000]
 [17000 21000 23000]
 [15000 18000 20000]]

Horizontal Sales:
[[10000 12000 15000 11000 13000 16000]
 [18000 20000 22000 17000 21000 23000]
 [14000 16000 19000 15000 18000 20000]]

Your Challenge:

  1. Join both department arrays using vstack().
  2. Join both arrays using hstack().
  3. Combine both arrays using stack() and check the resulting shape.
  4. Use dstack() to combine the arrays along the third axis.
  5. Print the shape of all four resulting arrays and compare the differences.

Key Takeaways

  • Joining combines two or more NumPy arrays into one.
  • concatenate() joins arrays along an existing axis.
  • axis=0 joins 2-D arrays row-wise.
  • axis=1 joins 2-D arrays column-wise.
  • stack() creates a new axis while joining arrays.
  • hstack(), vstack(), and dstack() join arrays horizontally, vertically, and along the third axis.

Frequently Asked Questions (FAQs)

1. What is NumPy Array Join in Python?

NumPy Array Join is the process of combining two or more NumPy Arrays into a single array. NumPy provides functions such as concatenate(), stack(), hstack(), vstack(), and dstack() to join arrays along different axes.

2. How do you join two arrays in NumPy?

You can use the np.concatenate() function to join two or more arrays along an existing axis.

Example: np.concatenate((arr1, arr2))

3. What is NumPy hstack() and how does it work?

NumPy hstack() is used to join two or more arrays horizontally. For 2-D arrays, it combines columns side by side, while for 1-D arrays, it joins their elements into a single 1-D array.

4. What is NumPy vstack() used for?

NumPy vstack() is used to join two or more arrays vertically. It places one array below another and is commonly used to combine rows of data.

5. What is the difference between NumPy hstack() and vstack()?

The main difference is that hstack() joins arrays horizontally (column-wise), whereas vstack() joins arrays vertically (row-wise). Both functions are useful for combining NumPy Arrays in different directions.

6. What is NumPy dstack() in Python?

NumPy dstack() is used to join two or more arrays along the third axis (depth-wise). It is commonly used to combine 2-D arrays and create a 3-D array.

7. What are the different methods for joining arrays in NumPy?

NumPy provides several methods for joining arrays, including concatenate(), stack(), hstack(), vstack(), and dstack(). Each method combines arrays differently depending on the required axis and dimensions.

8. What are the requirements for joining arrays in NumPy?

When joining arrays in NumPy, their dimensions and shapes must be compatible with the selected joining method. For example, with concatenate(), all dimensions except the joining axis must have matching sizes.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top