2016-09-20 84 views
1

我有如下因素結構的numpy的陣列匹配標準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'ModelType'<2

回答

1

通過使用PercentCntModelType的列逐列比較以及使用np.logical_and的連接,可以找到符合條件的所有行。這樣做,你實際上覆制了所有其他行,而不是刪除你想要刪除的行,但效果是一樣的。

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

條件

sb['PercentCnt'] >= 50 

保持的東西放在此列的條件,並

sb['ModelType'] >= 2 

是另一列相同的條件。

您可以np.logical_and組合這些:

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

最後,只是讓你想保留行:

sb[keep]