To delete elements in your dataset, usenp.delete
Syntax
np.delete(ndarray,elements,axis)
Let's start with this array in x
and delete the 2nd and 4th elements. To do so, we simply need to specify the positions in np.delete
.
Now lets say we have a 2D array. Removing elements will no longer be as easy as removing them from 1D arrays. If you remove just 1 element, the array will have improper dimensions. Hence, all that can be done is to remove entire columns or rows.
Let's start with a new array y
and delete the elements in the 2nd row.
Below, 0 and axis = 1
refers to the elements in the first column. Recall that axis defines whether you choose columns or rows.
y = np.delete(y, 0, axis = 1)
Using the same array, let's try a different combination to delete elements from the second row. Here, 0 and axis = 0
refers to the elements in the first row.
y = np.delete(y, 0, axis = 0)
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.