Course contentsShow
← Python
Lesson 917 of 1,06015. Data Science FundamentalsFree lesson

Combining and Splitting Multiple Arrays

You will learn to concatenate, stack, and split arrays to merge disparate data sources into unified numerical structures.

In this lesson, you will learn how to synthesize fragmented datasets by joining arrays together and how to decompose large structures into smaller, manageable chunks using NumPy.

Concatenating and Stacking

When working with disparate data sources, you often need to merge them into a single structure. np.concatenate allows you to join a sequence of arrays along an existing axis. For example, if you have two daily sensor readings of shape (10,), concatenating them along axis 0 results in a single array of shape (20,).

If you need to create a new dimension—such as stacking individual daily arrays into a collection of observations—np.stack is the preferred tool. While concatenate requires the arrays to have matching dimensions, stack joins them along a new axis.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Joining along the existing axis
joined = np.concatenate((a, b))  # Result: [1, 2, 3, 4, 5, 6]

# Creating a new dimension
stacked = np.stack((a, b), axis=0)  # Result: [[1, 2, 3], [4, 5, 6]]

Splitting Arrays

Conversely, you will often need to partition a unified array into smaller sub-arrays, perhaps to separate training and testing sets or to isolate specific features. The np.split function allows you to divide an array into multiple sub-arrays of equal size or at specific indices.

data = np.arange(9)
# Split into three equal-sized chunks
parts = np.split(data, 3) 
# Result: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]

# Split at specific indices
sections = np.split(data, [2, 5])
# Result: [array([0, 1]), array([2, 3, 4]), array([5, 6, 7, 8])]

These operations are essential for preprocessing data pipelines. By mastering the ability to reorganize your numerical structures, you can ensure your data is always in the exact shape required for your vectorized operations or Numba-optimized functions. Always ensure that the dimensions you are targeting align with your intended output, as NumPy will raise a ValueError if the shapes are incompatible for the chosen axis.

Key Takeaway: Use concatenate to extend arrays along existing dimensions, stack to increase dimensionality, and split to partition data into smaller segments for granular analysis.