1

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!

1
  • I gave one solution, but you may have more luck with a more specific title and code that immediately reproduces your error. Commented Nov 24, 2015 at 1:30

1 Answer 1

1

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'].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.