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

- Home
- Python Essentials for Data Analysis IToggle Dropdown
- 1.1 Getting started - Hello, World!
- 1.2 Variables
- 1.3 Data types
- 1.4 Printing
- 1.5 Lists
- 1.6 Dictionaries
- 1.7 Input function
- 1.8 Arithmetic operators
- 1.9 Comparison operators
- 1.10 Logical operators
- 1.11 Identity operators
- 1.12 Membership operators
- 1.13 Conditional statements (if-elif-else)
- 1.14 Importing modules
- 1.15 For loops
- 1.16 While loops

- Python Essentials for Data Analysis IIToggle Dropdown
- 2.1 Introduction to Functions in Python
- 2.2 Functions - Arguments
- 2.3 Functions with Return Values
- 2.4 Functions - A Fun Exercise!
- 2.5 Functions - Arbitrary Arguments (*args)
- 2.6 Functions - Arbitrary Keyword Arguments (**kwargs)
- 2.7 Recursive Functions
- 2.8 Lambda Expressions
- 2.9 Functions - More Exercises

- Data Analysis with PandasToggle Dropdown
- PD.1 Introduction to Pandas
- PD.2 Basics of Pandas
- PD.3 Finding and Describing data
- PD.4 Assigning Data
- PD.5 Manipulating Data
- PD.6 Handling Missing Data
- PD.7 Removing and adding data
- PD.8 Renaming data
- PD.9 Combining data
- PD.10 Using Pandas with other functions/mods
- PD.11 Data classification and summary
- PD.12 Data visualisation

- Data Analysis with NumPy
- NP.1 Introduction to NumPy
- NP.2 Create Arrays Using lists
- NP.3 Creating Arrays with NumPy Functions
- NP.4 Array Slicing
- What is array slicing?
- How to perform array slicing to retrieve elements
- Others
- Exercises
- Further Readings
- NP.5 Array Reshaping
- NP.6 Math with NumPy I
- NP.7 Combining 2 arrays
- NP.8 Adding elements to arrays
- NP.9 Inserting elements into arrays
- NP.10 Deleting elements from arrays
- NP.11 Finding unique elements and sorting
- NP.12 Math with NumPy II
- NP.13 Analysing data across arrays
- NP.14 NumPy Exercises

- Learning Resources
- Contact Us

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.

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.

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]

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,:]

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]

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.

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.

- Last Updated: Jun 24, 2024 9:14 AM
- URL: https://libguides.ntu.edu.sg/python
- Print Page

You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy.
Users will be personally liable for any infringement of Copyright and Licensing laws.

Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.