🚀 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.vsplit()

AI & DATA SCIENCE // np-vsplit

np.vsplit() splits an array into multiple sub-arrays vertically, row-wise, equivalent to np.split() with axis=0.

Syntax

np.vsplit(arr, indices_or_sections)

Deep Dive Course

vsplit() requires the array to have at least 2 dimensions, and divides it along its first axis, rows, either into N equal groups of rows, an integer argument, or at specific row-index boundaries, a list argument. It's the row-wise counterpart to np.hsplit(), and its name communicates the specific, common 'split by rows' intent more directly than the more general np.split(arr, ..., axis=0) call it's equivalent to.

1Understanding np.vsplit()

vsplit() requires the array to have at least 2 dimensions, and divides it along its first axis, rows, either into N equal groups of rows, an integer argument, or at specific row-index boundaries, a list argument. It's the row-wise counterpart to np.hsplit(), and its name communicates the specific, common 'split by rows' intent more directly than the more general np.split(arr, ..., axis=0) call it's equivalent to.

💡

vsplit() requires the array to already have at least 2 dimensions — calling it on a plain 1D array raises an error, since there's no concept of 'rows' to split by for a single-dimensional array.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
parts = np.vsplit(matrix, [1, 3])
print(len(parts))
print(parts[1])
localhost:3000

3Best Practices

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

1. Use vsplit() for the specific, common case of splitting a 2D array by rows, instead of the more generic np.split(arr, ..., axis=0)

2. Verify the array is at least 2D before calling vsplit(), since it doesn't apply to plain 1D arrays

3. Use explicit index positions when the rows need to be split unevenly, rather than an integer count that requires an even division

⚠️

Tip: vsplit() requires the array to already have at least 2 dimensions — calling it on a plain 1D array raises an error, since there's no concept of 'rows' to split by for a single-dimensional array.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)
Example 02Advanced Example
import numpy as np

matrix = np.arange(16).reshape(4, 4)
parts = np.vsplit(matrix, [1, 3])
print(len(parts))
print(parts[1])

Best Practices

  • Use vsplit() for the specific, common case of splitting a 2D array by rows, instead of the more generic np.split(arr, ..., axis=0)
  • Verify the array is at least 2D before calling vsplit(), since it doesn't apply to plain 1D arrays
  • Use explicit index positions when the rows need to be split unevenly, rather than an integer count that requires an even division

Interview Question

Why does calling np.vsplit() on a plain 1D array raise an error?

Hint: Think about what 'rows' means for an array with only one dimension.

vsplit() specifically splits an array along its first axis, treating each slice along that axis as a 'row', a concept that only makes sense for arrays with at least two dimensions. A 1D array has no second axis, so there's nothing analogous to a 'row' of multiple columns to preserve while splitting — vsplit() requires at least 2 dimensions and raises an error if given a 1D array, unlike the more general np.split(), which can operate along axis 0 of a 1D array without issue.

Exercises

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

matrix = np.arange(16).reshape(4, 4)
top, bottom = np.vsplit(matrix, 2)
print(top)

Frequently Asked Questions

Why does calling np.vsplit() on a plain 1D array raise an error?

vsplit() specifically splits an array along its first axis, treating each slice along that axis as a 'row', a concept that only makes sense for arrays with at least two dimensions. A 1D array has no second axis, so there's nothing analogous to a 'row' of multiple columns to preserve while splitting — vsplit() requires at least 2 dimensions and raises an error if given a 1D array, unlike the more general np.split(), which can operate along axis 0 of a 1D array without issue.

Related Functions

np-hsplitnp-splitnp-vstack