Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

What is array slicing?

Array slicing is similar to list slicing in Python. Array indexing also begins from `0`. However, since arrays can be multidimensional, we have to specify the slice for each dimension.

As we are mainly working with 2 dimensional arrays in this guide, we need to specify the row and column like what we do in a matrix. We can select a single element from an array or even a subarray containing multiple rows and columns of the original array.

To do array slicing, we use square brackets

Syntax

```nameofarray[start_row:end_row, start_col:end_col]
```

Example - This retrieves the element in row 1, column 2

```myArray[0:1, 1:2]
```

Note: If the start is not specified, it will be set to 0. If the end is not specified, it will run the length of the array in the dimension.

Let's create a simple array with shape (3,3) to work with. With a (3,3) shaped array, we know that its rows and columns can be referred to using their indices: 0, 1 and 2.

How to perform array slicing to retrieve elements

Retrieve elements from rows

To retrieve all the elements from the first row, specify the row parameter as `0`, and leave the column parameter empty.

Syntax

`myArray[0,:]`

You can also retrieve elements in a different row by replacing 0 with the proper index as long as they are within range.

Retrieve elements from columns

In this example, we are retrieving elements from the 2nd column. Thus we would need to specify the column parameter and leave the row parameter empty.

Syntax

`myArray[:,1]`

Retrieve elements from rows and columns

Suppose we want to retrieve a subarray consisting of the first 2 rows and all 3 columns. We can specify the parameters as below.

Syntax

`myArray[:2,:]`

Others

Reversing order of elements in arrays

Slicing can also be used to reverse the order of elements in arrays.
Let's first create an array to work with using `np.arange` and then use the syntax below.

Syntax

`myArray[::-1]`

Other information

Take note that slicing only allows you to view elements while still being linked to the original array.
This means that if we edit  myArray_new  then `myArray` will change as well.

Exercises

1. Use this array for the following practice:

`myArray = np.array([[11,12,13], [14,15,16], [17,18,19]])`

a. Get a subarray of the first row and first 2 columns. Your results should look like this:

`[11 12]`

b. Change all elements in 1st and second row to 0. Your results should look like this:

```[[ 0  0  0]
[ 0  0  0]
[17 18 19]]```

2. Create an array that contains `[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]` and reverse the order.

1a. Get a subarray of the first row and first 2 columns. Your results should look like this:

1b. change all elements in 1st and second row to 0

2. Create an array that contains `[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]` and reverse the order.