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

Зміст курсу

Ultimate NumPy

Ultimate NumPy

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

book
Assigning Values to Indexed Elements

Assigning values to specific elements or subarrays is useful for updating data, correcting errors, or applying conditions in datasets. This is especially helpful in tasks like replacing invalid entries, adjusting values for analysis, or modifying parts of an array for simulations and computations.

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.

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

Note

If you assign a value of a higher data type, like a float, to an element with a lower data type, like an integer, the value may be changed or cause an error. For example, assigning 3.5 to an integer element will store it as 3, losing the decimal part.

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.

The picture below shows the structure of the employee_data array used in the 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.

  1. Update the salary (first column) of the fourth employee to 60000.

  2. Use positive indexing to access and modify the value.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 9
toggle bottom row

book
Assigning Values to Indexed Elements

Assigning values to specific elements or subarrays is useful for updating data, correcting errors, or applying conditions in datasets. This is especially helpful in tasks like replacing invalid entries, adjusting values for analysis, or modifying parts of an array for simulations and computations.

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.

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

Note

If you assign a value of a higher data type, like a float, to an element with a lower data type, like an integer, the value may be changed or cause an error. For example, assigning 3.5 to an integer element will store it as 3, losing the decimal part.

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.

The picture below shows the structure of the employee_data array used in the 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.

  1. Update the salary (first column) of the fourth employee to 60000.

  2. Use positive indexing to access and modify the value.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 9
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt