numpy.vstack() in python (original) (raw)

Last Updated : 25 Apr, 2025

**numpy.vstack() is a function in NumPy used to stack arrays vertically (row-wise). It takes a sequence of arrays as input and returns a single array by stacking them along the vertical axis (axis 0).

**Example: Vertical Stacking of 1D Arrays Using numpy.vstack()

Python `

import numpy as geek

a = geek.array([ 1, 2, 3] ) print ("1st Input array : \n", a)

b = geek.array([ 4, 5, 6] ) print ("2nd Input array : \n", b)

res = geek.vstack((a, b)) print ("Output vertically stacked array:\n ", res)

`

Output

1st Input array : [1 2 3] 2nd Input array : [4 5 6] Output vertically stacked array: [[1 2 3] [4 5 6]]

**Explanation: The two 1D arrays a and b are stacked vertically using **np.vstack(), combining them into a 2D array where each input array forms a row.

Syntax

numpy.vstack(tup)

**Parameters:

**Return: [stacked ndarray] The stacked array of the input arrays.

Vertical Stacking of 2D Arrays Using numpy.vstack()

This code shows how to vertically stack two 2D arrays using **numpy.vstack(), resulting in a combined 2D array.

Python `

import numpy as geek

a = geek.array([[ 1, 2, 3], [ -1, -2, -3]] ) print ("1st Input array : \n", a)

b = geek.array([[ 4, 5, 6], [ -4, -5, -6]] ) print ("2nd Input array : \n", b)

res = geek.vstack((a, b)) print ("Output stacked array :\n ", res)

`

Output

1st Input array : [[ 1 2 3] [-1 -2 -3]] 2nd Input array : [[ 4 5 6] [-4 -5 -6]] Output stacked array : [[ 1 2 3] [-1 -2 -3] [ 4 5 6] [-4 -5 -6]]

**Explanation: Two 2D arrays a and b are vertically stacked, creating a new 2D array where each original array becomes a set of rows in the resulting array.

Similar Reads

Introduction







Creating NumPy Array













NumPy Array Manipulation


















Matrix in NumPy


















Operations on NumPy Array




Reshaping NumPy Array















Indexing NumPy Array






Arithmetic operations on NumPyArray










Linear Algebra in NumPy Array