O'Reilly logo

Learning pandas by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required