Joining Numpy Arrays

We can join two or more arrays in a single new array using the following:

  1. concatenate() method: Joining along with axis, unlike keys in SQL.
  2. stack method: Joining along a new axis, unlike keys in SQL.

In NumPy, axis=0 refers to the index (rows), and axis=1 refers to columns. Works differently in case of concatenate(), stack(), and aggregation (sum, mean, etc.) methods. The behavior depends on whether you are joining data (concatenating/stacking) or summarizing data (aggregating).

Let us see the comparison:

axis 0 and axis 1 comparsion numpy

Key Differences
  • np.concatenate: Joins a sequence of arrays along an existing axis.
    The result has the same number of dimensions as the input arrays.
    If you concatenate two 1D arrays of length 3, you get one 1D array of length 6.
  • np.stack: Joins a sequence of arrays along a new axis.
    The result has one more dimension than the input arrays.
    If you stack two 1D arrays of length 3, you get a 2D array of shape (2, 3) or (3, 2).

Joining Numpy Arrays with concatenate()

In NumPy, concatenate() is used to join two or more arrays along an existing axis. It does not create a new axis but merges arrays as per the specified dimension.

Syntax

numpy concatenate() method

Here,

  • (array1, array2, …) → A sequence (tuple/list) of arrays you want to join.
  • axis → The dimension along which arrays are joined. Default is 0 (rows).

Return: A new array formed by joining the input arrays along the specified axis.

Let us understand the axis parameter of the concatenate() method:

axis=0 → Vertical stacking (row-wise)

  • Concatenates along the first axis (rows).
  • Arrays are stacked on top of each other.
  • Requires that the arrays have the same number of columns.

axis=1 → Horizontal stacking (column-wise)

  • Concatenates along the second axis (columns).
  • Arrays are placed side by side.
  • Requires that the arrays have the same number of rows

Let us now see two examples:

Example 1

# Join NumPy Arrays with concatenate()
# Example 1

import numpy as np

# Create two arrays
n1 = np.array([[5, 10, 15, 20]])
n2 = np.array([[25, 30, 35, 50]])

print(n1)
print(n1.ndim)
print(n1.shape)

print(n2)
print(n2.ndim)
print(n2.shape)

# Stack vertically (row-wise)
# Concatenates along the first axis(rows)
res = np.concatenate((n1, n2), axis = 0)
print(res)
print(res.ndim)
print(res.shape)

# Stack horizontally (column-wise)
# Concatenates along the second axis(columns)
res = np.concatenate((n1, n2), axis = 1)
print(res)
print(res.ndim)
print(res.shape)

Output

[[ 5 10 15 20]]
2
(1, 4)
[[25 30 35 50]]
2
(1, 4)
[[ 5 10 15 20]
 [25 30 35 50]]
2
(2, 4)
[[ 5 10 15 20 25 30 35 50]]
2
(1, 8)

Example 2

# Join NumPy Arrays with concatenate()
# Example 2

import numpy as np

n1 = np.array([[1, 2],
               [3,4]])
n2 = np.array([[5, 6],
               [7,8]])

print(n1)
print(n1.ndim)
print(n1.shape)

print(n2)
print(n2.ndim)
print(n2.shape)

print("----------------------")

res = np.concatenate((n1, n2), axis=0)
print(res)
print(res.ndim)
print(res.shape)

res = np.concatenate((n1, n2), axis=1)
print(res)
print(res.ndim)
print(res.shape)

Output

[[1 2]
 [3 4]]
2
(2, 2)
[[5 6]
 [7 8]]
2
(2, 2)
----------------------
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
2
(4, 2)
[[1 2 5 6]
 [3 4 7 8]]
2
(2, 4)

Joining Numpy Arrays with Stack Methods

Join arrays in Numpy using the stack method, such as

  • stack(): Combines arrays along a new axis (adds dimension).
  • hstack(): Stacks arrays horizontally (column-wise, side by side).
  • vstack(): Stacks arrays vertically (row-wise, one below another).
  • dstack(): Stacks arrays depth-wise (along third axis).
  • column_stack(): Stacks 1D arrays as columns into a 2D array.

Each one differs by axis of stacking: adds a new axis, while hstack / vstack/ dstack/ column_stack arrange along existing ones

Let us see the examples one by one:

numpy.stack() in Python

Join Numpy arrays using the stack() method. It combines arrays along a new axis (adds dimension).

# Join NumPy arrays using stack() method

import numpy as np

n1 = np.array([1, 2, 3])
n2 = np.array([4, 5, 6])
print(n1.ndim)
print(n2.ndim)

# Join
# Here, axis = 0 is the default
res = np.stack((n1, n2), axis = 0)
print(res)
print(res.ndim)

# Join
res = np.stack((n1, n2), axis = 1)
print(res)

Output

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

numpy.hstack() in Python

Stack along rows using the hstack() method in Numpy:

# Join NumPy arrays using hstack() method

import numpy as np

n1 = np.array([1, 2, 3])
n2 = np.array([4, 5, 6])
print(n1.ndim)
print(n2.ndim)

# stack along rows
res = np.hstack((n1, n2))
print(res)
print(res.ndim)

Output

1
1
[1 2 3 4 5 6]
1

numpy.vstack() in Python

Stack along columns using the vstack() method in Numpy:

# Join NumPy arrays using vstack() method

import numpy as np

n1 = np.array([1, 2, 3])
n2 = np.array([4, 5, 6])
print(n1.ndim)
print(n2.ndim)

# stack along columns
res = np.vstack((n1, n2))
print(res)
print(res.ndim)

Output

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

numpy.dstack() in Python

Stack along depth, i.e., height, using the dstack() method in Numpy. The dstack() stacks arrays in sequence depth-wise (along the third axis):

  • It essentially adds a new third dimension and places arrays along it.
  • It stacks arrays along axis = 2 (the third dimension).
  • All input arrays must have the same shape along all but the third axis.
  • If you pass 1D arrays, they are first reshaped to 2D before stacking.

Let us see two examples:

Example 1

# Join NumPy arrays using dstack() method
# Example 1

import numpy as np

n1 = np.array([1, 2, 3])
n2 = np.array([4, 5, 6])
print(n1)
print(n1.ndim)
print(n1.shape)
print(n2)
print(n2.ndim)
print(n2.shape)

# stack along depth (height)
# stacks arrays along the third dimension
res = np.dstack((n1, n2))
print(res)
print(res.ndim)
print(res.shape)

Output

[1 2 3]
1
(3,)
[4 5 6]
1
(3,)
[[[1 4]
  [2 5]
  [3 6]]]
3
(1, 3, 2)

Example 2

# Join NumPy arrays using dstack() method
# Example 2

import numpy as np

n1 = np.array([[1, 2],
               [3, 4]])
n2 = np.array([[5, 6],
               [7, 8]])
print(n1)
print(n1.ndim)
print(n1.shape)
print(n2)
print(n2.ndim)
print(n2.shape)

# stack along depth (height)
# stacks arrays along the third dimension
res = np.dstack((n1, n2))
print(res)
print(res.ndim)
print(res.shape)

Output

[[1 2]
 [3 4]]
2
(2, 2)
[[5 6]
 [7 8]]
2
(2, 2)
[[[1 5]
  [2 6]]

 [[3 7]
  [4 8]]]
3
(2, 2, 2)

numpy.column_stack() in Python

Stack according to columns using the column_stack() method in Numpy:

# Join NumPy arrays using column_stack() method

import numpy as np
n1 = np.array([1, 2, 3])
n2 = np.array([4, 5, 6])

print(n1)
print(n1.ndim)
print(n1.shape)
print(n2)
print(n2.ndim)
print(n2.shape)

# Join arrays
res = np.column_stack((n1, n2))
print(res)
print(res.ndim)
print(res.shape)

Output

[1 2 3]
1
(3,)
[4 5 6]
1
(3,)
[[1 4]
 [2 5]
 [3 6]]
2
(3, 2)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

How to Iterate Numpy Arrays
Split Numpy Array
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment