.split()
Published Apr 4, 2025
Contribute to Docs
The .split()
function divides an array into multiple sub-arrays along a specified axis.
Syntax
numpy.split(array, indices_or_sections, axis=0)
array
: The input array to split.indices_or_sections
:- If an integer, it divides the array into equal-sized sub-arrays.
- If a list of indices, it splits at the specified positions.
axis
(Optional): The axis along which the array is split (default is0
, i.e., row-wise).
Example
The following example shows splitting arrays in different configurations using the .split()
function:
import numpy as np# Split a 1D array into 3 equal partsarr = np.array([11, 22, 33, 44, 55, 66])print(np.split(arr, 3))# Split a 2D array into 2 parts along rows (axis=0)nd = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])print(np.split(nd, 2, axis=0))# Split a 2D array at column indices [1, 2]print(np.split(nd, [1, 2], axis=1))
This produces the following output:
[array([11, 22]), array([33, 44]), array([55, 66])][array([[1, 2, 3],[4, 5, 6]]), array([[ 7, 8, 9],[10, 11, 12]])][array([[ 1],[ 4],[ 7],[10]]), array([[ 2],[ 5],[ 8],[11]]), array([[ 3],[ 6],[ 9],[12]])]
Codebyte Example
Run the following example to understand how to split the arrays using the .split()
function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours