Reputation: 473
If I had a random numpy array as shown below:
myArray:
[0 , 0 , 51.78 , 17],
[823. , 91.31 , 51.63 , 13],
[819. , 93.03 , 51.77 , 52],
[776.32 , 93.85 , 52.02 , 34],
[755.12 , 92.48 , 49.955 , 77],
[739.01 , 97.68 , 50.75 , 48],
[760.17 , 98.26 , 51.1 , 12],
[780.37 , 98.82 , 51.52 , 13],
[740.34 , 98.5 , 50.48 , 17],
[768.65 , 104.86 , 51.71 , 97]]
I was looking to remove the columns in which the first element is a 0, resulting in an array that follows:
myArray:
[51.78 , 17],
[51.63 , 13],
[51.77 , 52],
[52.02 , 34],
[49.955 , 77],
[50.75 , 48],
[51.1 , 12],
[1.52 , 13],
[50.48 , 17],
[51.71 , 97]]
NOTE
I am aware of np.delete
, I am not sure if I can implement this given any random array. I am looking for a method in which, if given a random array that has 0's in the first row, I can implement the same function to any array, not just the given example above.
Any tips would be greatly appreciated!
Upvotes: 0
Views: 60
Reputation: 214957
Use Boolean Indexing and the condition would be arr[0] != 0
:
arr[:, arr[0] != 0]
#array([[ 51.78 , 17. ],
# [ 51.63 , 13. ],
# [ 51.77 , 52. ],
# [ 52.02 , 34. ],
# [ 49.955, 77. ],
# [ 50.75 , 48. ],
# [ 51.1 , 12. ],
# [ 51.52 , 13. ],
# [ 50.48 , 17. ],
# [ 51.71 , 97. ]])
Upvotes: 3