1

Here's my problem: I'm dealing with output from different receivers, and they are listed by number in column 0 of my array. I'm trying to find the indices that correspond to certain receiver values that show up. For my code below, I was trying to find all indices that had a value of 6.

My problem is that for the output (print) I'm only getting [ ], as if there are no indices that correspond to values for receiver 6. I've seen the data file and know this to be incorrect. The data text file is a 4x22000ish array. Any help would be greatly appreciated. thanks.

from numpy import *

data = loadtxt("/home/***")
s,t,q = data[:,0], data[:,2], data[:,3]                         
t,q = loadtxt("/home/***", usecols = (2,3), unpack=True)

indices = []
for index, value in enumerate(data[:,0]):
    if value == '6':
        indices.append(index)

print indices

1 Answer 1

1
numpy.nonzero(data[:,0]==6)[0]

data[:,0]==6 returns an array of booleans, 1 when the condition is true, 0 when it is false

numpy.nonzero returns the index of nonzero elements inside of a container

you may also be interested to know that you can do things like

data[data[:,0]==6,2]

to grab all the elements from the 2nd column when the first column is zero

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.