Splitting arrays

Arrays can also be split into multiple arrays along the horizontal, vertical, and depth axes using the np.hsplit(), np.vsplit(), and np.dsplit() functions. We will only look at the np.hsplit() function as the others work similarly.

The np.hsplit() function takes the array to split as a parameter, and either a scalar value to specify the number of arrays to be returned, or a list of column indexes to split the array upon.

If splitting into a number of arrays, each array returned will have the same count of columns. The source array must have a number of columns that is a multiple of the specified value.

To demonstrate this, we will use the following array with four columns and three rows:

In [70]:
# sample array
a = np.arange(12).reshape(3, ...

Get Learning pandas now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.