Riggun Riggun - 2 months ago 40
Python Question

Numpy delete multiple rows matching criteria

I have a numpy array of folowing structure

sb = np.genfromtxt(open('HomePage/TodayList.txt', 'rb'),
delimiter=',', skiprows=0,
dtype=[('DataBase', np.str_, 16), ('Mode', np.str_, 16),
('SMB', np.str_, 16),('Desc', np.str_, 128),
('Res', np.str_, 16), ('RightCnt', np.float64),
('PercentCnt', np.float64), ('ModelType', np.float64)])


The 6th column
'PercentCnt'
which can be accessed by name
'PercentCnt'
contains numbers from 0 to 50
the 7th column
'ModelType'
contains numbers from 0 to 5 so i need to remove or delete array rows which match these criteria
'PercentCnt'<50
and
'ModelType'<2
.

Answer

You can find all rows matching your criteria by use of a column-wise comparison for PercentCnt and ModelType and connection using np.logical_and. Doing that, you actually copy all other rows rather than to delete the ones you wanted to get rid of, but the effect is the same.

sb = sb[np.logical_and(sb["PercentCnt"]>=50, sb["ModelType"]>=2)]