I have a database with the following array:
array = np.array([[1,4,1,1,0,0],[4,5,6,3,0,0],[1,4,5,6,0,0]])
I'm working with the datas in column, for example:
array[:,2]
out: array([1, 6, 5])
I would like to create a new array without the columns of 0
To do that, I created an array of zeros:
array_zero = np.zeros((3,6))
Then, I compare each index with this array:
index = 0
while (array[:,index] != array_zero[:,0]):
index = index + 1
When I have the index of the first column of 0, I know that the following columns will be columns of 0.
So, I would want to create my new array like this:
array_new = np.array(3,index + 1)
for i in range(index+ 1):
array_new[:,i] = array[:,i]
In the while loop, I have the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I tried the following:
while ((array[:,index] != array_zero[:,0]).all):
index = index + 1
or
while ((array[:,index] != array_zero[:,0]).any):
index = index + 1
And I have the following error:
IndexError: index 6 is out of bounds for axis 1 with size 6
Here is the code:
import numpy as np
index = 0
array = np.array([[1,4,1,1,0,0],[4,5,6,3,0,0],[1,4,5,6,0,0]])
array_zero = np.zeros((3,6))
while (array[:,index] != array_zero[:,0]):
index = index + 1
array_new = np.array(3,index + 1)
for i in range(index+ 1):
array_new[:,i] = array[:,i]
Thank you for your help
np.any(array)instead. That tells you whether any of the values is true (!= 0).