Kelly
Kelly

Reputation: 121

Selecting rows from python array

I have two arrays. Let's say they look like this:

    time1 = [ 1 2 3 ] and time2 = [ 2 4 6]
            [ 4 5 6 ]         
            [ 7 8 9 ]         

I would like to select only the rows from time1 for which the first column is within the range of time2. For example, from this data set, I would plot the [4 5 6] row, because 4 is in the range of 2 - 6. I am trying to select the rows from array time1 like this:

selectedtimes = time1(any(time1[:,0] < time2[-1]) and any(time1[:,0] > time2[0]))

I am currently receiving the object not callable error (shown below), and am quite stuck. Is there a better way to rewrite this line?

'numpy.ndarray' object is not callable

Help appreciated!

Upvotes: 1

Views: 138

Answers (2)

Chaker
Chaker

Reputation: 1207

Using for and if:

>>> time1 = ((1,2,3),(4,5,6),(7,8,9))
>>> time2 = (2,4,6)
>>> for x in time1:
...  if x[0] in time2:
...   print x
... 
(4, 5, 6)
>>> 

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

You can use numpy.logical_and here:

>>> np.logical_and(time1[:,0] > time2[0], time1[:,0] < time2[-1] )
array([False,  True, False], dtype=bool)
>>> time1[np.logical_and(time1[:,0] > time2[0], time1[:,0] < time2[-1] )]
array([[4, 5, 6]])

Upvotes: 3

Related Questions