# NumPy Array Basics - Querying, Slicing, Combining, and Splitting Arrays

``````

In :

import sys
print(sys.version)
import numpy as np
print(np.__version__)

``````
``````

3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
1.9.2

``````

In this video we’ll be covering querying, slicing, combining, and splitting arrays. Now this information is really important because it will consistently come up when we’re working in pandas. Overall it’s a pretty simple idea and it’s fairly declarative.

``````

In :

np.random.seed(10)

``````

First let’s generate some random arrays. We’ll generate some that are 1 dimension, 2 dimensional, and 3 dimensional.

``````

In :

ar = np.arange(12)
ar

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

``````
``````

In :

ar2 = np.random.random_integers(12, size=12)
ar2

``````
``````

Out:

array([10,  5,  1,  2, 12, 10,  1,  2, 11,  9, 10,  1])

``````
``````

In :

ndim_ar = np.arange(12).reshape(3,4)
ndim_ar

``````
``````

Out:

array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])

``````
``````

In :

ndim_ar2 = np.random.random_integers(12, size=(3,4))
ndim_ar2

``````
``````

Out:

array([[11,  9,  7,  5],
[ 4,  1,  5, 12],
[ 7,  9, 12, 11]])

``````
``````

In :

ndim_ar3d = np.arange(8).reshape(2,2,2)
ndim_ar3d

``````
``````

Out:

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

[[4, 5],
[6, 7]]])

``````

Querying 1 dimensional arrays is easy, we just perform the lookup like we would a regular array.

``````

In :

ar

``````
``````

Out:

5

``````
``````

In :

ar[5:]

``````
``````

Out:

array([ 5,  6,  7,  8,  9, 10, 11])

``````
``````

In :

ar[1:6:2]

``````
``````

Out:

array([1, 3, 5])

``````
``````

In :

ar[-1:-6:-2]

``````
``````

Out:

array([11,  9,  7])

``````
``````

In :

ndim_ar

``````
``````

Out:

array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])

``````

Querying 2 dimensional arrays is a bit more interesting, we use commas to separate the axis.

``````

In :

ndim_ar[:,1:3]

``````
``````

Out:

array([[ 1,  2],
[ 5,  6],
[ 9, 10]])

``````
``````

In :

ndim_ar[1:3,:]

``````
``````

Out:

array([[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])

``````
``````

In :

ndim_ar[1:3,1:3]

``````
``````

Out:

array([[ 5,  6],
[ 9, 10]])

``````
``````

In :

ndim_ar3d

``````
``````

Out:

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

[[4, 5],
[6, 7]]])

``````

Things get even more interesting with 3+ dimensions, obviously it’s a lot to keep track in your head. but We just go dimension by dimension.

We’ll get the first dimension, then all the items in the second dimension, then everything beyond the first item in the 3rd dimension.

``````

In :

ndim_ar3d[0,:,1:]

``````
``````

Out:

array([,
])

``````

Now that we’ve got some experience querying, let’s go over combining different arrays.

Now let’s stack the first two arrays vertically, we’ll do that with vstack. We can do the same with multidimensional arrays.

``````

In :

np.vstack((ar,ar2))

``````
``````

Out:

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
[10,  5,  1,  2, 12, 10,  1,  2, 11,  9, 10,  1]])

``````
``````

In :

np.vstack((ndim_ar, ndim_ar2))

``````
``````

Out:

array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[11,  9,  7,  5],
[ 4,  1,  5, 12],
[ 7,  9, 12, 11]])

``````

Now you can probably guess what horizontal stacking is. That would be hstack.

``````

In :

np.hstack((ndim_ar, ndim_ar2))

``````
``````

Out:

array([[ 0,  1,  2,  3, 11,  9,  7,  5],
[ 4,  5,  6,  7,  4,  1,  5, 12],
[ 8,  9, 10, 11,  7,  9, 12, 11]])

``````
``````

In :

ar3 = np.hstack((ar,ar2))
ar3

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 10,  5,  1,  2, 12,
10,  1,  2, 11,  9, 10,  1])

``````
``````

In :

ar

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

``````

Of course we aren’t limited to two arrays, we can stack as many as we like.

We can also use concatenate to join them together. We can specify the axis to do so.

``````

In :

np.concatenate((ar,ar2))

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 10,  5,  1,  2, 12,
10,  1,  2, 11,  9, 10,  1])

``````

we can stack them dimensionally with dstack. Now we’ve got a 3 dimensional join of these two two dimensional arrays.

``````

In :

ndim_ar3 = np.concatenate((ndim_ar,ndim_ar2), axis=0)
ndim_ar3

``````
``````

Out:

array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[11,  9,  7,  5],
[ 4,  1,  5, 12],
[ 7,  9, 12, 11]])

``````
``````

In :

np.concatenate((ndim_ar,ndim_ar2), axis=1)

``````
``````

Out:

array([[ 0,  1,  2,  3, 11,  9,  7,  5],
[ 4,  5,  6,  7,  4,  1,  5, 12],
[ 8,  9, 10, 11,  7,  9, 12, 11]])

``````
``````

In :

ndim_ar3d = np.dstack((ndim_ar,ndim_ar2))
ndim_ar3d

``````
``````

Out:

array([[[ 0, 11],
[ 1,  9],
[ 2,  7],
[ 3,  5]],

[[ 4,  4],
[ 5,  1],
[ 6,  5],
[ 7, 12]],

[[ 8,  7],
[ 9,  9],
[10, 12],
[11, 11]]])

``````

We can split them back with the dsplit command. This gives us back our original arrays.

``````

In :

ndim_ar3d_split = np.dsplit(ndim_ar3d, 2)
ndim_ar3d_split

``````
``````

Out:

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

[[ 4],
[ 5],
[ 6],
[ 7]],

[[ 8],
[ 9],
,
]]), array([[,
[ 9],
[ 7],
[ 5]],

[[ 4],
[ 1],
[ 5],
],

[[ 7],
[ 9],
,
]])]

``````
``````

In :

ndim_ar3d_split.flatten()

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

``````
``````

In :

ndim_ar3d_split.flatten()

``````
``````

Out:

array([11,  9,  7,  5,  4,  1,  5, 12,  7,  9, 12, 11])

``````
``````

In :

ar

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

``````

We can do the same with hsplit and vsplit.

``````

In :

np.hsplit(ar3,2)

``````
``````

Out:

[array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]),
array([10,  5,  1,  2, 12, 10,  1,  2, 11,  9, 10,  1])]

``````
``````

In :

ndim_ar3

``````
``````

Out:

array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[11,  9,  7,  5],
[ 4,  1,  5, 12],
[ 7,  9, 12, 11]])

``````
``````

In :

np.vsplit(ndim_ar3,2)

``````
``````

Out:

[array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]]), array([[11,  9,  7,  5],
[ 4,  1,  5, 12],
[ 7,  9, 12, 11]])]

``````

Now that's all that I wanted to cover. I will mention that there isa dedicated matrix formation but going over this is outside the scope of this tutorial.

At this point we’ve covered a lot of numpy. Now I’m sure you’re thinking this isn’t quite applied but a lot of this functionality will be embedded into pandas. so it’s great to review. With these videos we’ve covered a lot of what you’ll find yourself using in numpy but don’t be afraid to dive into the documentation yourself. There are some things that I haven’t covered like specific matrix types, linear algebra but these are outside the scope of this introduction. If you’ve got any questions please feel free to ask in the discussion.