I have a ndarray which looks like this example:
[['000' '0.0008303259945444978']
['001' '0.001097105216902647']
['010' '0.009235959101850126']
['011' '0.00047129154937779666']
['100' '0.018205469077740434']
['101' '0.0013647687750767113']
['110' '0.94056678166667']
['111' '0.028228298617837884']]
I need to pick values from the second column, whose corresponding values in the first column matches particular criteria.
Example criteria: second and third position of the string (in first column) are equal to zero. If this is true, take a copy of the corresponding values in the second column and create new ndarray
with them.
CodePudding user response:
You can do such a thing by indexing. So, just as an example, if you want rows in which the first column have two zeros in 2nd and 3rd place, the values must be dividable by 100
. In this regard, if we want to use NumPy, we can convert the array type to floats to be checked if it can be divided by 100
. This will get a Boolean mask array hat can be used for indexing to select the expected rows:
mask = a.astype(np.float64)[:, 0] % 100 == 0
# [ True False False False True False False False]
a[mask, 1]
or
a[a[:, 0].astype(np.float64) % 100 == 0, 1]
It was just an example based on your example. You can create your needed mask and work on that.
CodePudding user response:
With a normal comprehension list, you can do:
[j for i, j in arr if i[1]=='0' and i[2]=='0']
# ['0.0008303259945444978', '0.018205469077740434']
You can change the criteria as required.