繁体   English   中英

Numpy删除符合条件的多行

[英]Numpy delete multiple rows matching criteria

我有一个numpy数组的结构

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)])

可以通过名称'PercentCnt'访问的第6列'PercentCnt'包含从0到50的数字第7列'ModelType'包含从0到5的数字,因此我需要删除或删除符合这些条件'PercentCnt'<50数组行'PercentCnt'<50'ModelType'<2

条件

sb['PercentCnt'] >= 50

保持这个列和条件的条件

sb['ModelType'] >= 2

另一列是相同的。

您可以将这些与np.logical_and结合使用:

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

最后,只需保留您希望保留的行:

sb[keep]

您可以通过使用PercentCntModelType的列式比较以及使用np.logical_and连接来查找符合条件的所有行。 这样做,你实际上复制了所有其他行,而不是删除你想要删除的行,但效果是相同的。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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