Contenido del Curso
Ultimate NumPy
Ultimate NumPy
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.
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)
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 as3
, losing the decimal part.
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)
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:
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.
-
Update the salary (first column) of the fourth employee to
60000
. -
Use positive indexing to access and modify the value.
¡Gracias por tus comentarios!
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.
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)
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 as3
, losing the decimal part.
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)
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:
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.
-
Update the salary (first column) of the fourth employee to
60000
. -
Use positive indexing to access and modify the value.
¡Gracias por tus comentarios!