Home > database >  Extract a row from a Python numpy array by condition
Extract a row from a Python numpy array by condition

Time:01-31

I have an array (called "attractors") which looks like this:

[['0000000000' '0.0' '0.0']
 ['0000000001' '0.0' '1.0']
 ['0000000010' '0.0' '2.0']
...........................

I want to create new array which contains all rows where the third column was 0 in the original array. I try the following:

print(attractors[attractors[: , 2] == 0][: , 0])

but I receive the following error:

            json export to visualize state transition diagram with compression
 - tests.py:247: FutureWarning: elementwise comparison failed; 
    returning scalar instead, but in the 
        future will perform elementwise comparison 
              print(attractors[attractors[: , 2] == 0][: , 0])

If I put brackets on the condition, like this:

print(attractors[attractors[: , 2] == "0"][: , 0])

then the error does not appear, but the results is not what I expected (only empty brackets [])

CodePudding user response:

isn't it because you have strings of format '0.0'

so maybe you should do something like this

print(attractors[attractors[: , 2] == '0.0'][: , 0])

CodePudding user response:

You can use list comprehension:

[x for x in attractors if x[2] == 0]

If it's a string you can change the equality to == '0.0'

  • Related