Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Assigning Values to Indexed Elements | Indexing and Slicing
course content

Course Content

Ultimate NumPy

Assigning Values to Indexed ElementsAssigning Values to Indexed Elements

It is often necessary to assign a certain value to a specific element of an array or even to values in a subarray of elements.

First of all, we can assign a value to an indexed element of an array. Here is the general syntax to accomplish this in 1D arrays: array[i] = n, where i is a certain index and n is the value to be assigned.

In 2D arrays, we have the following syntax: array[i, j] = n, where i and j are the row and column indices, respectively. For higher-dimensional arrays, the number of indices corresponds to the number of dimensions.

Let’s look at an example:

As you can see, everything is rather simple here.

Note

Assigning values of higher data types to elements with lower data types (e.g., assigning a float value to an integer element) can cause unexpected results or even an error.

Higher data types are those that can store a larger range of values and often occupy more memory.

Here is an example of such a scenario:

No exception was thrown, however, the first element was assigned the value of 10 instead of 10.2. The float value was converted to an integer since that's the dtype of the array.

Task

You are managing a dataset of employee information, where each row represents an employee, and the columns represent their salary and performance score. The dataset is stored in the employee_data array array. Your task is to update the salary (first column) of the fourth employee to 60000 (use positive indices).

Everything was clear?

Section 2. Chapter 10
toggle bottom row
course content

Course Content

Ultimate NumPy

Assigning Values to Indexed ElementsAssigning Values to Indexed Elements

It is often necessary to assign a certain value to a specific element of an array or even to values in a subarray of elements.

First of all, we can assign a value to an indexed element of an array. Here is the general syntax to accomplish this in 1D arrays: array[i] = n, where i is a certain index and n is the value to be assigned.

In 2D arrays, we have the following syntax: array[i, j] = n, where i and j are the row and column indices, respectively. For higher-dimensional arrays, the number of indices corresponds to the number of dimensions.

Let’s look at an example:

As you can see, everything is rather simple here.

Note

Assigning values of higher data types to elements with lower data types (e.g., assigning a float value to an integer element) can cause unexpected results or even an error.

Higher data types are those that can store a larger range of values and often occupy more memory.

Here is an example of such a scenario:

No exception was thrown, however, the first element was assigned the value of 10 instead of 10.2. The float value was converted to an integer since that's the dtype of the array.

Task

You are managing a dataset of employee information, where each row represents an employee, and the columns represent their salary and performance score. The dataset is stored in the employee_data array array. Your task is to update the salary (first column) of the fourth employee to 60000 (use positive indices).

Everything was clear?

Section 2. Chapter 10
toggle bottom row
some-alt