How to add 2 columns in numpy ndarray? [duplicate]

随声附和 提交于 2021-01-29 11:43:02

问题


I have an numpy ndarray like below:

[[1 9 1 1]
 [9 3 1 1]
 [1 9 9 1]
 [8 2 4 7]]

I want to add last 2 columns values to get below result

[[1 9 2]
 [9 3 2]
 [1 9 10]
 [8 2 11]]

回答1:


Sum, then drop the last column

myArray[:, -2] = myArray[:,-2] + myArray[:,-1] 
myArray = myArray[:,:-1]


来源:https://stackoverflow.com/questions/61092371/how-to-add-2-columns-in-numpy-ndarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!