Conteúdo do Curso
Ultimate NumPy
Ultimate NumPy
Assigning 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:
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)
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:
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.
Tarefa
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).
Obrigado pelo seu feedback!
Assigning 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:
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)
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:
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.
Tarefa
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).
Obrigado pelo seu feedback!
Assigning 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:
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)
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:
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.
Tarefa
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).
Obrigado pelo seu 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:
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)
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:
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.
Tarefa
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).