Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove column of array via indexing in Python

I'm trying to remove an observation in an array via indexing. What I have is:

import numpy as np
test = np.ones([1, 1001])

What I want to do is return an array that is the same as test, but having removed the 5th observation (ie, test[0:4 AND 6:]). Is there a simple way to do this?

like image 957
mike Avatar asked Mar 05 '26 01:03

mike


1 Answers

You could use slicing and hstack:

In [18]: test_ex5 = np.hstack((test[:,:5],test[:,6:]))

In [19]: test.shape
Out[19]: (1, 1001)

In [20]: test_ex5.shape
Out[20]: (1, 1000)

Note that your indexing is off by one: test[0:4 AND 6:] would delete two elements instead of one.

like image 157
NPE Avatar answered Mar 07 '26 15:03

NPE



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!