所以我习惯于像这样组合数据帧掩码:final_mask = mask1 & mask2
但是如果我想组合多个蒙版怎么办?例如,列表:[掩码1,掩码2,掩码3,掩码4,...,掩码20]
您可以使用pandas cookbook solution ,最后一段为 reduce
:
df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]})
print (df)
AAA BBB CCC
0 4 10 100
1 5 20 50
2 6 30 -30
3 7 40 -50
mask1 = df.AAA <= 5.5
mask2 = df.BBB == 10.0
mask3 = df.CCC > -40.0
masks = [mask1, mask2, mask3]
mask = functools.reduce(lambda x,y: x & y, masks)
print (df[mask])
AAA BBB CCC
0 4 10 100
来自ayhan
的另一个解决方案关于 1d
掩码的评论(掩码是 Series
):
mask = np.logical_and.reduce(masks)
print (df[mask])
AAA BBB CCC
0 4 10 100
如ayhan指出,第一个解决方案也适用于 2D 蒙版:
mask1 = df <= 5.5
mask2 = df < 1.0
mask3 = df > -40.0
masks = [mask1, mask2, mask3]
mask = functools.reduce(lambda x,y: x & y, masks)
print (mask)
AAA BBB CCC
0 False False False
1 False False False
2 False False True
3 False False False
<小时/>
mask = np.logical_and.reduce(masks)
print (mask)
ValueError: cannot copy sequence with size 4 to array axis with dimension 3
我是一名优秀的程序员,十分优秀!