Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subtract two columns from the same array and put the value in their own single column array with numpy?

Lets say i have a single array of 3x4 (3 rows, 4 columns) for example

import numpy as np

data = [[0,5,0,1], [0,5,0,1], [0,5,0,1]]

data = np.array(data)

print(data)

[[0 5 0 1]
 [0 5 0 1]
 [0 5 0 1]]

and i want to subtract column 4 from column 2 and have the values in their own, named, 3x1 array like this

print(subtraction)

[[4]
 [4]
 [4]]

how would i go about this in numpy?

like image 369
CodingKid Avatar asked Oct 21 '25 12:10

CodingKid


1 Answers

result = (data[:, 1] - data[:, 3]).reshape((3, 1))
like image 114
avysk Avatar answered Oct 23 '25 00:10

avysk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!