Two-Dimensional Array Indexing
Learn how to access individual elements, rows, and columns in two-dimensional NumPy arrays. Understand row and column indexes, negative indexing, element modification, and practical matrix operations.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand the structure of a two-dimensional NumPy array.
- Identify row indexes and column indexes.
- Access individual elements using row and column positions.
- Select complete rows and columns.
- Use negative indexes to access elements from the end.
- Modify individual elements and complete rows.
- Combine indexing with slicing to select portions of a matrix.
- Apply two-dimensional indexing to a practical dataset.
2. What Is a Two-Dimensional Array?
A two-dimensional array stores data in rows and columns. It is commonly used to represent tables, matrices, student marks, sales records, and other structured information.
Each element has two index positions: one for its row and one for its column. NumPy uses zero-based indexing, which means the first row and first column both have index 0.
Example: Student Marks Table
Samadhan stores the marks of three students across four subjects. Each row represents a student, while each column represents a subject.
| Row / Student | Subject 0 | Subject 1 | Subject 2 | Subject 3 |
|---|---|---|---|---|
| Row 0 | 78 | 85 | 90 | 88 |
| Row 1 | 82 | 79 | 94 | 91 |
| Row 2 | 75 | 88 | 84 | 86 |
3. Understanding Row and Column Indexes
NumPy indexes begin at zero. For a two-dimensional array, the first number represents the row index and the second number represents the column index.
The general syntax for accessing an element is:
marks[1, 1] selects the
element at row index 1 and column index 1. It returns the value
79 in this example.
4. Accessing Individual Elements
To retrieve a single value from a two-dimensional array, provide both its row index and its column index.
| Expression | Meaning | Output |
|---|---|---|
marks[0, 0] |
First row, first column | 78 |
marks[0, 2] |
First row, third column | 90 |
marks[1, 3] |
Second row, fourth column | 91 |
marks[2, 1] |
Third row, second column | 88 |
5. Selecting Complete Rows
To select an entire row, specify the row index and use a colon for the column selection. The colon means that all columns should be selected.
You can also use the shorter form marks[0] to
select the first row.
6. Selecting Complete Columns
To select a complete column, use a colon for the row selection and specify the required column index.
marks[:, 2], the first
colon selects every row and the number 2 selects column index 2.
7. Using Negative Indexes
Negative indexes allow you to access rows and columns from the end of the array. The index -1 represents the last row or column, depending on which position it is used in.
| Expression | Description |
|---|---|
marks[-1, -1] |
Selects the last element of the last row. |
marks[-1, :] |
Selects the complete last row. |
marks[:, -1] |
Selects the complete last column. |
marks[0, -1] |
Selects the last element in the first row. |
8. Modifying Elements in a Two-Dimensional Array
You can update an element by assigning a new value to its row and column position.
Updating a Complete Row
When replacing a complete row, the number of replacement values must match the row's length and the values must be compatible with the array's data type.
9. Combining Indexing and Slicing
You can combine individual indexes and slices to select a specific part of a matrix. This is particularly useful when working with large tables.
| Expression | Selected Data |
|---|---|
data[0:2, 1:3] |
First two rows and columns 1 and 2. |
data[-2:, :] |
Last two rows and every column. |
data[:3, :2] |
First three rows and first two columns. |
10. Common Indexing Errors
| Error | Reason | Solution |
|---|---|---|
| IndexError: index out of bounds | The requested row or column index does not exist. | Check the array shape and use valid indexes. |
| Incorrect row and column order | The row and column positions have been reversed. | Use array[row, column]. |
| Forgetting the comma | Two-dimensional indexing needs separate row and column selections. | Write array[1, 2] instead of treating both indexes as one. |
| Confusing index and position | NumPy starts indexing at zero. | Remember that the first row and column have index 0. |
| Incorrect assignment length | The replacement row does not match the required shape. | Provide compatible values with the expected dimensions. |
array.shape to
check the number of rows and columns before accessing elements.
11. Practical Activity: Student Marks Analysis
Activity: Access and Update Student Marks
Samiksha is reviewing marks for three students across four subjects. Use two-dimensional indexing to retrieve selected marks, display a complete row and column, and update one value.
Expected Output
12. Practice Exercises
Use this array to complete the following exercises:
- Access the value 180 using row and column indexes.
- Select the first row.
- Select the third row.
- Select the second column.
- Access the last value in the array using negative indexes.
- Select the first two rows and the first three columns.
- Change the value at row index 1 and column index 2 to 175.
- Print the shape of the array.
Challenge Exercise
Pradnya has a matrix containing monthly sales for four products. Create a two-dimensional NumPy array with at least four rows and three columns. Then:
- Display the second product's complete row.
- Display the first month's sales for every product.
- Retrieve the last value of the last row.
- Update one selected sales value.
- Extract a rectangular section of the array using slicing.
Summary
In this lesson, we learned how two-dimensional NumPy arrays
organize data into rows and columns. We used
array[row, column] to access individual elements,
selected complete rows and columns, and explored negative indexes.
We also learned how to update elements, combine indexing with slicing, inspect array dimensions, and recognize common indexing errors. These skills are essential when analyzing tables, matrices, and structured datasets.
Practice the examples using different arrays to build confidence with row and column indexing.
In the next lesson, you will learn how Boolean indexing and fancy indexing can select array elements using conditions and lists of indexes.