参考文章:https://blog.csdn.net/qq_36523839/article/details/82318061
1. numpy.logical_and(逻辑与)
# 所有数据皆从0开始索引
import numpy as np
df = np.array([[1,0,1],[2,2,0,],[0,3,2]])
# 原始矩阵
print(df)
输出结果:[[1 0 1]
[2 2 0]
[0 3 2]]
# 获得第一列和第二列的布尔值,并将两者做与操作
result = np.logical_and(df[:,0]>0,df[:,1]>0)
print(df[:,0]>0,df[:,1]>0)
print(result)
输出结果:
[ True True False] [False True True]
[False True False]
2. numpy.logical_or(逻辑或)
# 或操作一个为真全为真
import numpy as np
df = np.array([[1,0,1],[2,2,0,],[0,3,2]])
result = np.logical_or(df[:,0]>0,df[:,1]>0)
print(df[:,0]>0,df[:,1]>0)
print(result)
输出结果:
[ True True False] [False True True]
[ True True True]
3. numpy.logical_not(逻辑非)
import numpy as np
df = np.array([[1,0,1],[2,2,0,],[0,3,2]])
result = np.logical_not(df[:,0]>0)
result2 = np.logical_not(df[:,1]>0)
print(df[:,0]>0,df[:,1]>0)
print(result,result2)
输出结果:
[ True True False] [False True True]
[False False True] [ True False False]