Betty Norris
Betty Norris

Reputation: 57

Value Error in python numpy

I am trying to remove rows with ['tempn', '0', '0'] in it. Rows with ['tempn', '0'] should not be removed however.

 my_input =  array([['temp1', '0', '32k'],
       ['temp2', '13C', '0'],
       ['temp3', '0', '465R'],
       ['temp4', '0', '0'],
       ['temp5', '22F', '0'],
       ['temp6', '0', '-15C'],
       ['temp7', '0', '0'],
       ['temp8', '212K', '1'],
       ['temp9', '32C', '0'],
       ['temp10', '0', '19F']], 
  dtype='|S2'), array([['temp1', '15K'],
                       ['temp2', '0'],
                       ['temp3', '16C'],
                       ['temp4', '0'],
                       ['temp5', '22F'],
                       ['temp6', '0'],
                       ['temp7', '457R'],
                       ['temp8', '305K'],
                       ['temp9', '0'],
                       ['temp10', '0']],  dtype='|S2')]

Based on a previous question, I tried

 my_output = []
 for c in my_input:
     my_output.remove(c[np.all(c[:, 1:] == '1', axis = 1)])

I sprung up with a value error however, saying truth value of an array of more than one element is ambiguous. Thanks!

Upvotes: 1

Views: 70

Answers (1)

Amyunimus
Amyunimus

Reputation: 1633

The trick is to compare the elements individually rather than both at the same time, which is probably why you were getting the error.

final_out = []
for item1 in my_input:
    my_output = []    
    for item2 in item1:
        try:
            if item2[1] != '0' or item2[2] != '0':
                my_output.append(item2)
        except IndexError:
            my_output.append(item2)
    final_out.append(np.array(my_output))

This will preserve your list of array structure while removing ['tempn', '0', '0'].

Upvotes: 1

Related Questions