Search code examples
pythonnumpymatrixmultiple-columns

Can I access data in Nx3 Matrix for other column with specific value


Say I have

data=np.array([[30,0.109,1],
               [25,0.517,2],
               [22,0.174,1],
               [35,0.812,3],
               [45,0.215,4],
               [40,0.111,4],
               [50,0.095,4]])

Doing

A = data1[:,2]
print(A)

Will give me

[1. 2. 1. 3. 4. 4. 4.]

Is there a command so I can get the values from other columns (1 or 2) if column 3 has a value of 4 In my mind it would look like this B=data[:,1,[2]=4] So B should print out [0.812, 0.215, 0.111]

Is this possible? The reason I need it is for graphical display. The number in column 3 corresponds to a type of bacteria (I.e. all 4's are the same bacteria), from which I need to plot the other values "bound" to that type of bacteria.

Hope its possible, and all the best


Solution

  • You need to use boolean indexing:

    out = data[data[:, 2]==4, 1]
    

    Output:

    array([0.215, 0.111, 0.095])