0

I have a (2, 500) numpy array named county_data. I want to iterate over the first column, check if each value is equal to a number someNumber, and if so, attach its row to a list called temp.

Here is my code so far:

for entry in county_data:       
    if entry[0] == someNumber:  
        temp.append(entry)  
    print temp

Here's the error I get:

   if entry[0] == code:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I don't quite know what this means, and the a.any() and a.all() functions don't seem to do what I want with each row in the array. How can I edit my code to check that the first entry in each row of the array matches someNumber?

1 Answer 1

3

Don't do that. Instead, access all the rows at once (i.e., vectorize your code):

temp = county_data[county_data[:, 0] == someNumber]
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.