
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Joining Arrays
Joining Arrays in NumPy
Joining arrays in NumPy refers to the process of combining two or more arrays into a single array. The result may vary depending on the dimensions and axes along which the arrays are joined.
NumPy provides several functions for joining of arrays along different axes, they are −
- The np.concatenate() Function
- The np.stack() Function
- The np.hstack() Function
- The np.vstack() Function
Using concatenate() Function
The NumPy concatenate() function joins a sequence of arrays along a specified axis. The arrays must have the same shape, except in the dimension corresponding to the axis along which they are being concatenated. Following is the basic syntax −
np.concatenate((array1, array2, ...), axis=0)
Where,
- array1, array2, ... − It is the sequence of arrays to be concatenated. These arrays should have the same shape along all axes except for the one specified by axis.
- axis − It is the axis along which the arrays will be joined. The default value is 0, meaning the arrays will be concatenated along rows (for 2D arrays).
Example: Concatenating 1D Arrays
In the following example, we are concatenating two 1D arrays, "array1" and "array2" along the default axis (axis 0) −
import numpy as np # Create two 1D arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Concatenate along the default axis (axis=0) result = np.concatenate((array1, array2)) print("Concatenated Array:", result)
The resulting array is a single 1D array containing all elements from both input arrays −
Concatenated Array: [1 2 3 4 5 6]
Example: Concatenating 2D Arrays Along Different Axes
Here, we are concatenating two 2D arrays along different axes in NumPy using the concatenate() function −
import numpy as np # Create two 2D arrays array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Concatenate along axis 0 (row-wise) result_axis_0 = np.concatenate((array1, array2), axis=0) print("Concatenated along Axis 0:\n", result_axis_0) # Concatenate along axis 1 (column-wise) result_axis_1 = np.concatenate((array1, array2), axis=1) print("Concatenated along Axis 1:\n", result_axis_1)
Following is the output obtained −
Concatenated along Axis 0: [[1 2] [3 4] [5 6] [7 8]] Concatenated along Axis 1: [[1 2 5 6] [3 4 7 8]]
Example: Concatenating Arrays of Different Dimensions
In here, we reshape the 1D array "array1" into a 2D array with the given shape, making it compatible for concatenation with the 2D array "array2" along axis "0" −
import numpy as np # Create a 1D and a 2D array array1 = np.array([1, 2, 3]) array2 = np.array([[4, 5, 6], [7, 8, 9]]) # Reshape array1 to make it 2D for concatenation along axis 0 array1_reshaped = array1.reshape(1, -1) # Concatenate along axis 0 result = np.concatenate((array1_reshaped, array2), axis=0) print("Concatenated Array:\n", result)
This will produce the following result −
Concatenated Array: [[1 2 3] [4 5 6] [7 8 9]]
Stacking Arrays Using stack() Function
The NumPy stack() function joins a sequence of arrays along a new axis, which you specify. This function is useful when you need to preserve the original dimensions of the arrays while adding a new axis for stacking. Following is the syntax −
np.stack((array1, array2, ...), axis=0)
Where,
- array1, array2, ... − It is the sequence of arrays to be stacked. These arrays must have the same shape.
- axis − It is the axis along which the arrays will be stacked. The default value is 0, which means the arrays are stacked along a new first axis.
Example: Stacking 2D Arrays
In the following example, we are stacking two 2D arrays along a new third axis. This creates a 3D array with a particular shape −
import numpy as np # Create two 2D arrays array1 = np.array([[1, 2], [3, 4]]) array2 = np.array([[5, 6], [7, 8]]) # Stack along a new third axis (axis 2) stacked_array = np.stack((array1, array2), axis=2) print("Stacked Array along Axis 2:\n", stacked_array) print("Shape of Stacked Array:", stacked_array.shape)
The new dimension represents the depth of the array, where corresponding elements from the original arrays are stacked together −
Stacked Array along Axis 2: [[[1 5] [2 6]] [[3 7] [4 8]]] Shape of Stacked Array: (2, 2, 2)
Example: Stacking Multiple Arrays
In this example, three 1D arrays are stacked along a new first axis using the stack() function −
import numpy as np # Create three 1D arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) array3 = np.array([7, 8, 9]) # Stack along the first axis (new axis 0) stacked_array = np.stack((array1, array2, array3), axis=0) print("Stacked Array:\n", stacked_array) print("Shape of Stacked Array:", stacked_array.shape)
The result is a 2D array where each original array forms a row in the new array −
Stacked Array: [[1 2 3] [4 5 6] [7 8 9]] Shape of Stacked Array: (3, 3)
Horizontal Stacking Using hstack() Function
The NumPy hstack() function stacks arrays in sequence horizontally (column-wise). This means that the arrays are concatenated along their second dimension (axis 1 for 2D arrays). For 1D arrays, they are simply concatenated along the single available axis.
Following is the syntax −
np.hstack((array1, array2, ...))
Where, array1, array2, ... is a sequence of arrays that you want to stack horizontally. All arrays must have the same shape along all but the second axis.
Example
In the example below, we are stacking two 2D arrays, "array1" and "array2" horizontally, resulting in a new array where the columns of "array2" are placed to the right of the columns of "array1" −
import numpy as np # Create two 2D arrays array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[7, 8, 9], [10, 11, 12]]) # Horizontally stack the arrays hstacked_array = np.hstack((array1, array2)) print("Horizontally Stacked 2D Array:\n", hstacked_array)
Following is the output of the above code −
Horizontally Stacked 2D Array: [[ 1 2 3 7 8 9] [ 4 5 6 10 11 12]]
Vertical Stacking Using vstack() Function
The NumPy vstack() function stacks arrays in sequence vertically (row-wise). This means that the arrays are concatenated along their first dimension (axis 0 for 2D arrays). For 1D arrays, they are treated as rows and stacked on top of each other, resulting in a 2D array.
Following is the syntax −
np.vstack((array1, array2, ...))
Where, array1, array2, ... is a sequence of arrays that you want to stack vertically. All arrays must have the same shape along all but the first axis.
Example
In the following example, we are stacking two 2D arrays, "array1" and "array2" vertically, resulting in a new array where the rows of "array2" are placed below the rows of "array1" −
import numpy as np # Create two 2D arrays array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[7, 8, 9], [10, 11, 12]]) # Vertically stack the arrays vstacked_array = np.vstack((array1, array2)) print("Vertically Stacked 2D Array:\n", vstacked_array)
The output obtained is as shown below −
Vertically Stacked 2D Array: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
Splitting Arrays After Joining
After joining arrays, you might want to split them back into their original or differently shaped parts. NumPy provides several functions for splitting arrays −
- np.split() function − Splits an array into multiple sub-arrays as specified by the indices or sections.
- np.array_split() function − Similar to np.split() function, but allows splitting into unequal sub-arrays.
- np.hsplit() function − Splits an array horizontally (column-wise).
- np.vsplit() function − Splits an array vertically (row-wise).
- np.dsplit() function − Splits an array along the third axis (depth-wise), used for 3D arrays.
Array splitting is the process of dividing an array into multiple sub-arrays. This operation is the inverse of array joining.
Example
In the following example, we are splitting the vertically stacked array into two equal parts using the np.vsplit() function, effectively reversing the vertical stacking operation −
import numpy as np # Vertically stack two arrays array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[7, 8, 9], [10, 11, 12]]) vstacked_array = np.vstack((array1, array2)) # Split the array back into the original arrays split_arrays = np.vsplit(vstacked_array, 2) print("Split Arrays:") for arr in split_arrays: print(arr)
After executing the above code, we get the following output −
Split Arrays: [[1 2 3] [4 5 6]] [[ 7 8 9] [10 11 12]]