Merging NumPy Arrays

NumPy offers powerful tools for array manipulation, and understanding how to merge arrays is essential for data analysis and scientific computations. Experiment with the provided examples to deepen your understanding!

Bernd Fischer

Bernd Fischer

11. Oct 2024 · 3 min read

Merging NumPy Arrays

Installing NumPy

If numpy is not yet installed, it can be installed via pip:

pip install numpy

Creating Arrays and Merging with numpy.concatenate

With numpy.concatenate, arrays can be combined along a specific axis. We start with small arrays and merge them row-wise and column-wise.

Example 1: Merging Simple Arrays Along the Row Axis (axis=0)

import numpy as np

# Zwei einfache Arrays erstellen
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Visualisierung der Arrays
print("Array 1:", array1)
print("Array 2:", array2)

# Mergen entlang der Zeilenachse (axis=0)
merged_array = np.concatenate((array1, array2), axis=0)

# Ausgabe des gemergten Arrays
print("Gemergtes Array entlang der Zeilenachse:", merged_array)

Output:

Array 1: [1 2 3]
Array 2: [4 5 6]
Gemergtes Array entlang der Zeilenachse: [1 2 3 4 5 6]

Here, two one-dimensional arrays are combined along the row axis into a single array.

Example 2: Merging Multidimensional Arrays Along the Row Axis (axis=0)

# Mehrdimensionale Arrays erstellen
array3 = np.array([[1, 2], [3, 4]])
array4 = np.array([[5, 6], [7, 8]])

# Visualisierung der Arrays
print("Array 3:\n", array3)
print("Array 4:\n", array4)

# Mergen entlang der Zeilenachse (axis=0)
merged_2d_array = np.concatenate((array3, array4), axis=0)

# Ausgabe des gemergten Arrays
print("Gemergtes Array entlang der Zeilenachse:\n", merged_2d_array)

Output:

Array 3:
 [[1 2]
 [3 4]]
Array 4:
 [[5 6]
 [7 8]]
Gemergtes Array entlang der Zeilenachse:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]

In this example, two 2x2 arrays are combined along the row axis, resulting in a 4x2 array.

Merging Arrays with numpy.hstack and numpy.vstack

The functions hstack and vstack allow simple merging of arrays horizontally (column-wise) or vertically (row-wise).

Example 3: Merging Arrays Horizontally with hstack

# Horizontales Mergen von 2x2 Arrays
array5 = np.array([[1, 2], [3, 4]])
array6 = np.array([[5, 6], [7, 8]])

# Visualisierung der Arrays
print("Array 5:\n", array5)
print("Array 6:\n", array6)

# Horizontales Mergen (spaltenweise)
hstacked_array = np.hstack((array5, array6))

# Ausgabe des gemergten Arrays
print("Horizontal gemergtes Array:\n", hstacked_array)

Output:

Array 5:
 [[1 2]
 [3 4]]
Array 6:
 [[5 6]
 [7 8]]
Horizontal gemergtes Array:
 [[1 2 5 6]
 [3 4 7 8]]

Here, two arrays are combined column-wise, resulting in an array with four columns.

Example 4: Merging Arrays Vertically with vstack

# Vertikales Mergen von 1x4 Arrays
array7 = np.array([1, 2, 3, 4])
array8 = np.array([5, 6, 7, 8])

# Visualisierung der Arrays
print("Array 7:", array7)
print("Array 8:", array8)

# Vertikales Mergen (zeilenweise)
vstacked_array = np.vstack((array7, array8))

# Ausgabe des gemergten Arrays
print("Vertikal gemergtes Array:\n", vstacked_array)

Output:

Array 7: [1 2 3 4]
Array 8: [5 6 7 8]
Vertikal gemergtes Array:
 [[1 2 3 4]
 [5 6 7 8]]

In this example, two one-dimensional arrays are combined row-wise, resulting in a 2x4 array.

Merging Arrays with numpy.stack

With numpy.stack, arrays can be combined along a new axis. This adds an additional dimension, which is particularly useful for multidimensional data.

Example 5: Merging Arrays Along a New Axis

# Mehrdimensionale Arrays erstellen
array9 = np.array([[1, 2], [3, 4]])
array10 = np.array([[5, 6], [7, 8]])

# Visualisierung der Arrays
print("Array 9:\n", array9)
print("Array 10:\n", array10)

# Mergen entlang einer neuen Achse
stacked_array = np.stack((array9, array10), axis=0)

# Ausgabe des gemergten Arrays
print("Gemergtes Array entlang einer neuen Achse:\n", stacked_array)

Output:

Array 9:
 [[1 2]
 [3 4]]
Array 10:
 [[5 6]
 [7 8]]
Gemergtes Array entlang einer neuen Achse:
 [[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

In this example, an additional dimension (axis 0) is added, combining the arrays in a higher dimension.