简体   繁体   中英

Python - Select row in NumPy array where multiple conditions are met

My program contains many different NumPy arrays, with various data inside each of them. An example of an array is:

x = [5, 'ADC01', Input1, 25000], # Where [TypeID, Type, Input, Counts]
    [5, 'ADC01', Input2, 40000]

From separate arrays I can retrieve the value of Type and Input . I then need to say

Counts = x[0,3] where Type = 'ADC01' and Input = 'Input2'

Obviously it would not be wrote like this. For the times that I have only needed to satisfy one condition, I have used:

InstType_ID = int(InstInv_Data[InstInv_Data[:,glo.inv_InstanceName] == Instrument_Type_L][0,glo.inv_TypeID])

Here, it looks in array(InstInv_Data) at the 'InstanceName' column and finds a match to Instrument_Type. It then assigns the 'TypeID' column to InstType_ID. I basically want to add an and statement so it also looks for another matching piece of data in another column.

Edit: I just thought that I could try and do this in two separate steps. Returning both Input and Counts columns where Type-Column = Type . However, I am unsure of how to actually return two columns, instead of a specific one. Something like this:

Intermediate_Counts = (InstDef_Data[InstDef_Data[:,glo.i_Type] == Instrument_Type_L][0,(glo.i_Input, glo.i_Counts])

You could use a & b to perform element-wise AND for two boolean arrays a , b :

selected_rows = x[(x[:,1] == 'ADC01') & (x[:,2] == 'Input2')]

Similarly, use a | b a | b for OR and ~a for NOT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM