简体   繁体   中英

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?

result = (data[:, 1] - data[:, 3]).reshape((3, 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM