In this tutorial, we will look at the numpy hsplit() function and its usage with the help of some examples.

## What does numpy hsplit() do?

The numpy hsplit() function is used to split a numpy array into multiple sub-arrays horizontally (column-wise). Pass the input array and the number of sub-arrays as arguments. The following is the syntax:

import numpy as np # split array column-wise (horizontally) sub_arrays = np.hsplit(arr, indices_or_sections)

It returns a list of numpy arrays created from the split (the sub-arrays).

Let’s look at some examples of using the numpy hsplit() function.

## 1. Split 1d numpy array horizontally

A 1d numpy array can be thought of as an array with a single row and multiple columns. Let’s apply the `np.hsplit()`

function to split a 1d array column-wise.

import numpy as np # create a 1d numpy array arr = np.array([1, 0, 1, 2, 2, 2]) # split the array into 2 subarrays horizontally sub_arrays = np.hsplit(arr, 2) # display the sub_arrays print(sub_arrays)

Output:

[array([1, 0, 1]), array([2, 2, 2])]

You can see that the resulting list has two sub-arrays (each 1d) created from splitting the input array horizontally.

Since the array is of length 6, we can also split it into 3 or 6 sub-arrays.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# split the array into 3 subarrays horizontally sub_arrays = np.hsplit(arr, 3) # display the sub_arrays print(sub_arrays)

Output:

[array([1, 0]), array([1, 2]), array([2, 2])]

You can see that the returned list has 3 sub-arrays created from the column-wise split of the input array.

## 2. Split 2d numpy array horizontally

We can similarly split 2d numpy arrays. For example, let’s split a (3, 4) numpy array into two (3, 2) numpy arrays by splitting it horizontally (column-wise).

# create a 2d numpy array arr = np.array([[1, 2, 3, 4], [2, 0, 0, 2], [3, 1, 1, 0]]) # split the array into 2 subarrays horizontally sub_arrays = np.hsplit(arr, 2) # display the sub_arrays sub_arrays

Output:

[array([[1, 2], [2, 0], [3, 1]]), array([[3, 4], [0, 2], [1, 0]])]

The resulting sub-arrays are of shape (3, 2).

Let’s now split the same input array into 4 sub-arrays of (3, 1) by splitting all the columns.

# create a 2d numpy array arr = np.array([[1, 2, 3, 4], [2, 0, 0, 2], [3, 1, 1, 0]]) # split the array into 4 subarrays horizontally sub_arrays = np.hsplit(arr, 4) # display the sub_arrays sub_arrays

Output:

[array([[1], [2], [3]]), array([[2], [0], [1]]), array([[3], [0], [1]]), array([[4], [2], [0]])]

We get four (3, 1) sub-arrays.

## Using numpy split() with axis=1

Alternatively, you can perform a horizontal split with the numpy `split()`

function. Just pass `axis=1`

along with the input array and the number of sections to split it into.

Let’s split the above 2d array into two sub-arrays horizontally but using the numpy `split()`

function this time.

# create a 2d numpy array arr = np.array([[1, 2, 3, 4], [2, 0, 0, 2], [3, 1, 1, 0]]) # split the array into 2 subarrays horizontally sub_arrays = np.split(arr, 2, axis=1) # display the sub_arrays sub_arrays

Output:

[array([[1, 2], [2, 0], [3, 1]]), array([[3, 4], [0, 2], [1, 0]])]

For more on the numpy hsplit() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**

Tutorials on numpy arrays –

- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()