Python split array by value

Python split array by value
Onyejiaku Theophilus Chidalu

Overview

In python, an array is a data structure used to store multiple items of the same type together. They are helpful when dealing with many values of the same data type. An array needs to import the array module for explicit declaration.

Method

The numpy.array_split() method in python is simply used to split an array into multiple sub-arrays of equal size. It takes these parameters:

  • ary: This represents the input array. This parameter is required.
  • indices_or_section: This represents the number of splits to be returned. This parameter is required.
  • axis: This is the axis along which the values are appended. This parameter is optional.

In this shot, we want to see how we can easily access the newly split arrays using their index position.

Code

import numpy as np

array1 = np.array([1, 2, 3, 4])

# splitting the array into three

new_array1 = np.array_split(array1, 3)

print('The split arrays are:', new_array1)

# to access the first split array

print('The first split array is:', new_array1[0])

# to access the second split array

print('The second split array is: ', new_array1[1])

# to access the second split array

print('The third split array is: ', new_array1[2])

Explanation

  • Line 1: we imported the numpy module
  • Line 3: we created an array, array1, using the numpy.array() method
  • Line 5: we split the created array into three using the numpy.array_split() method and assigned it to a variable called new_array1
  • Line 6: we printed the newly split array new_array1
  • Line 9: we returned the first array present in the newly split arrays
  • Line 11: we returned the second array present in the newly split arrays
  • Line 13: we returned the third array present in the newly split arrays

RELATED TAGS

array

numpy

python

communitycreator

CONTRIBUTOR

Python split array by value
Onyejiaku Theophilus Chidalu

Python split array by value
Onyejiaku Theophilus Chidalu

Overview

The numpy.array_split() method in Python is used to split an array into multiple sub-arrays of equal size.

In Python, an array is a data structure that is used to store multiple items of the same type together. Arrays are useful when dealing with many values of the same data type. An array needs to explicitly import the array module for declaration.

Syntax

The syntax for the split() method is as follows:

numpy.array_split(array, indices_or_section, axis=0)

Parameters

ParameterDescription
array The input array (required).
indices_or_section The value for the number of splits we want (required).
axis The axis along which the values are appended (optional).

Return value

The return value of numpy.array_split() is an array that contains the number of splits, as specified when the method is called.

Example 1

In the example below, we use the numpy.array_split() method to split an array into two parts.

Code

import numpy as np

array1 = np.array([1, 2, 3, 4])

# splitting the array into two

new_array1 = np.array_split(array1, 2)

print('Tne newly splitted arrays are: ', new_array1)

Example: Splitting the array

Example 2

In the next example, we use the numpy.array_split() method to split an array into three parts.

Code

import numpy as np

array1 = np.array([1, 2, 3, 4])

# splitting the array into three

new_array1 = np.array_split(array1, 3)

print('The newly splitted arrays are:', new_array1)

Example: Splitting the array

RELATED TAGS

numpy

python

communitycreator

CONTRIBUTOR

Python split array by value
Onyejiaku Theophilus Chidalu

How do you separate values from an array in Python?

Use the array_split() method, pass in the array you want to split and the number of splits you want to do.

How do you split a list by value in Python?

Python String split() Method The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do you split a 2D array in Python?

array_split() method in Python is used to split a 2D array into multiple sub-arrays of equal size.

How do you split elements in an array?

The split() method splits a string into an array of substrings. The split() method returns the new array.