Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numpy add one column to other columns and remove

Say I have an 2d numpy array like this

[[1,2,3],
 [4,5,6],
 [7,8,9]]

I then want to convert it to

[[3,4],
 [9,10],
 [15,16]]

This could be a variable number of columns, I want to add the first column to every other column and remove it as well afterwards.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

a = a[..., [0]] + a[..., 1:]

The …is for this to work with N dimensional arrays

Means -> Sum the [0] column to all the columns after the first one.

If you are using just a matrix you can use:

a = a[:, [0]] + a[:, 1:]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading