🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.vstack()

AI & DATA SCIENCE // np-vstack

np.vstack() stacks a sequence of arrays vertically, row by row, equivalent to concatenating them along axis 0 after ensuring they're at least 2D.

Syntax

np.vstack((arr1, arr2, ...))

Deep Dive Course

vstack() is a convenience wrapper: it treats each input as a row, promoting a 1D array to a single row if needed, and stacks them on top of each other, requiring all inputs to have the same number of columns. For already-2D arrays, np.vstack((a, b)) is exactly equivalent to np.concatenate((a, b), axis=0), but vstack()'s name communicates the specific, common 'stack rows' intent more directly than a generic axis argument does.

1Understanding np.vstack()

vstack() is a convenience wrapper: it treats each input as a row, promoting a 1D array to a single row if needed, and stacks them on top of each other, requiring all inputs to have the same number of columns. For already-2D arrays, np.vstack((a, b)) is exactly equivalent to np.concatenate((a, b), axis=0), but vstack()'s name communicates the specific, common 'stack rows' intent more directly than a generic axis argument does.

💡

vstack() is especially convenient for combining several 1D arrays into rows of a 2D matrix, since it automatically treats each 1D input as one row, something concatenate() alone doesn't do without first reshaping them.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
localhost:3000

2Practical Example

Here is a real-world application of np.vstack() showing how it is used in production NumPy code.

editor.html
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.vstack((a, b)))
localhost:3000

3Best Practices

Follow these guidelines when working with np.vstack():

1. Use vstack() for the common case of stacking arrays as new rows, instead of the more generic np.concatenate(..., axis=0)

2. Ensure every input array has the same number of columns, the same size along axis 1, before calling vstack()

3. Use vstack() to combine several 1D arrays into a single 2D matrix, one row per input, without manually reshaping each one first

⚠️

Tip: vstack() is especially convenient for combining several 1D arrays into rows of a 2D matrix, since it automatically treats each 1D input as one row, something concatenate() alone doesn't do without first reshaping them.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
Example 02Advanced Example
import numpy as np

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

Best Practices

  • Use vstack() for the common case of stacking arrays as new rows, instead of the more generic np.concatenate(..., axis=0)
  • Ensure every input array has the same number of columns, the same size along axis 1, before calling vstack()
  • Use vstack() to combine several 1D arrays into a single 2D matrix, one row per input, without manually reshaping each one first

Interview Question

How does np.vstack() handle two separate 1D arrays, and why does the result end up 2D?

Hint: Think about what vstack() does to a 1D input before stacking it.

vstack() first treats each 1D input array as if it were a single row of a 2D matrix, effectively giving it a shape of (1, n) internally, and then stacks those rows on top of each other along a new vertical axis. Combining two 1D arrays of length 3 this way produces a 2D result with shape (2, 3), two rows, three columns, since each original array became one row of the output, rather than the elements simply being concatenated end-to-end into a longer 1D array.

Exercises

MediumPractice using np.vstack() in a real scenario.
View Solution
import numpy as np

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

Frequently Asked Questions

How does np.vstack() handle two separate 1D arrays, and why does the result end up 2D?

vstack() first treats each 1D input array as if it were a single row of a 2D matrix, effectively giving it a shape of (1, n) internally, and then stacks those rows on top of each other along a new vertical axis. Combining two 1D arrays of length 3 this way produces a 2D result with shape (2, 3), two rows, three columns, since each original array became one row of the output, rather than the elements simply being concatenated end-to-end into a longer 1D array.

Related Functions

np-hstacknp-stacknp-concatenate