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
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookAssigning 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:

12345678910
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10 to the first element of array_1d array_1d[0] = 10 print(array_1d) print('-' * 14) array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Assigning 8 to the element in the second row and column of array_2d array_2d[1, 1] = 8 print(array_2d)
copy

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.

Here is an example of such a scenario:

12345
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10.2 to the first element of array_1d array_1d[0] = 10.2 print(array_1d)
copy

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
test

Swipe to show code editor

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).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 10
toggle bottom row

bookAssigning 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:

12345678910
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10 to the first element of array_1d array_1d[0] = 10 print(array_1d) print('-' * 14) array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Assigning 8 to the element in the second row and column of array_2d array_2d[1, 1] = 8 print(array_2d)
copy

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.

Here is an example of such a scenario:

12345
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10.2 to the first element of array_1d array_1d[0] = 10.2 print(array_1d)
copy

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
test

Swipe to show code editor

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).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 10
toggle bottom row

bookAssigning 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:

12345678910
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10 to the first element of array_1d array_1d[0] = 10 print(array_1d) print('-' * 14) array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Assigning 8 to the element in the second row and column of array_2d array_2d[1, 1] = 8 print(array_2d)
copy

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.

Here is an example of such a scenario:

12345
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10.2 to the first element of array_1d array_1d[0] = 10.2 print(array_1d)
copy

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
test

Swipe to show code editor

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).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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:

12345678910
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10 to the first element of array_1d array_1d[0] = 10 print(array_1d) print('-' * 14) array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Assigning 8 to the element in the second row and column of array_2d array_2d[1, 1] = 8 print(array_2d)
copy

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.

Here is an example of such a scenario:

12345
import numpy as np array_1d = np.array([1, 4, 6, 2]) # Assigning 10.2 to the first element of array_1d array_1d[0] = 10.2 print(array_1d)
copy

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
test

Swipe to show code editor

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).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 10
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt