Reputation: 306
So I currently have an array that looks like this,
[[1. 2. 1. 0.]
[3. 4. 1. 0.]
[6. 5. 0. 1.]
[8. 7. 0. 1.]
[3. 5. 1. 0.]
[2. 9. 1. 0.]]
Where the third column will indicate a 1 if the first column is greater than the second column, and vise versa with column four. I am trying to remove duplicates in columns three and four for only the number one directly below the first one. I basically want it to look like this.
[[1. 2. 1. 0.]
[3. 4. 0. 0.]
[6. 5. 0. 1.]
[8. 7. 0. 0.]
[3. 5. 1. 0.]
[2. 9. 0. 0.]]
Below is my current code, how do I make this change?
import numpy as np
arr = np.array((1,2,3,4,6,5,8,7,3,5,2,9))
arr = np.reshape(arr, (-1,2))
arr = np.append(arr,np.zeros([len(arr),1]),1)
arr = np.append(arr,np.zeros([len(arr),1]),1)
arr[:, 2] = np.where(arr[:, 0] > arr[:, 1], arr[:, 2], 1)
arr[:, 3] = np.where(arr[:, 0] < arr[:, 1], arr[:, 3], 1)
Upvotes: 0
Views: 64
Reputation: 36289
You can update the columns by using the 'less than' operator:
arr[1:, 2] = arr[:-1, 2] < arr[1:, 2]
And similarly for the fourth column.
Upvotes: 1