SVenkatesh
SVenkatesh

Reputation: 63

iterating over one column of a numpy multidimensional array?

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?

Upvotes: 0

Views: 202

Answers (1)

ecatmur
ecatmur

Reputation: 157314

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

temp = county_data[county_data[:, 0] == someNumber]

Upvotes: 3

Related Questions