This may be a silly question, but I've just started using numpy and I have to figure out how to perform some simple operations.
Suppose that I have the 2x3 array
array([[1, 3, 5],
   [2, 4, 6]])
And that I want to perform some operation on the first column, for example subtract 1 to all the elements to get
array([[0, 3, 5],
   [1, 4, 6]])
How can I perform such an operation?
arr
# array([[1, 3, 5],
#        [2, 4, 6]])
arr[:,0] = arr[:,0] - 1     # choose the first column here, subtract one and 
                            # assign it back to the same column
arr
# array([[0, 3, 5],
#        [1, 4, 6]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With