python の配列を各要素ごとに論理演算する場合、numpy の
- logical_not
- logical_and
- logical_or
- logical_xor
が使えます。
通常の配列、numpy の ndarray で動作します。配列じゃなく、スカラでも動作します。
Scipy のサイト には明記されていませんが、pandas の Series、DataFrame でも動きました。
- 目次 -
スポンサーリンク
スカラ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import numpy as np x = True y = False np.logical_not(x) # not x と同じ #-------- # False #-------- np.logical_and(x, y) # x and y または x & y と同じ #-------- # False #-------- np.logical_or(x, y) # x or y または x | y と同じ #-------- # True #-------- np.logical_xor(x, y) # x ^ y と同じ #-------- # True #-------- |
通常の配列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import numpy as np a = [True, True, False] b = [True, False, False] np.logical_not(a) #----------------------------------------- # array([False, False, True], dtype=bool) #----------------------------------------- np.logical_and(a, b) # a and b と同じ(型が ndarray になる点が異なる) #----------------------------------------- # array([ True, False, False], dtype=bool) #----------------------------------------- np.logical_or(a, b) # a or b と同じ(型が ndarray になる点が異なる) #----------------------------------------- # array([ True, True, False], dtype=bool) #----------------------------------------- np.logical_xor(a, b) #----------------------------------------- # array([False, True, False], dtype=bool) #----------------------------------------- |
numpy 配列 (ndarray)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import numpy as np A = np.array([True, True, False]) B = np.array([True, False, False]) np.logical_not(A) # ~A と同じ #----------------------------------------- # array([False, False, True], dtype=bool) #----------------------------------------- np.logical_and(A, B) # A & B と同じ #----------------------------------------- # array([ True, False, False], dtype=bool) #----------------------------------------- np.logical_or(A, B) # A | B と同じ #----------------------------------------- # array([ True, True, False], dtype=bool) #----------------------------------------- np.logical_xor(A, B) # A ^ B と同じ #----------------------------------------- # array([False, True, False], dtype=bool) #----------------------------------------- |
pandas の Series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import numpy as np from pandas import Series SA = Series([True, True, False]) SB = Series([True, False, False]) np.logical_not(SA) # ~SA と同じ #---------------- # 0 False # 1 False # 2 True #---------------- np.logical_and(SA, SB) # SA & SB と同じ #---------------- # 0 True # 1 False # 2 False #---------------- np.logical_or(SA, SB) # SA | SB と同じ #---------------- # 0 True # 1 True # 2 False #---------------- np.logical_xor(SA, SB) # SA ^ SB と同じ #---------------- # 0 False # 1 True # 2 False #---------------- |
pandas の DataFrame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import numpy as np from pandas import DataFrame DA = DataFrame({'col1':[True, True, False],'col2':[True, True, False]}) #----------------- # col1 col2 # 0 True True # 1 True True # 2 False False #----------------- DB = DataFrame({'col1':[True, False, False],'col2':[True, False, False]}) #----------------- # col1 col2 # 0 True True # 1 False False # 2 False False #----------------- np.logical_not(DA) # ~DA と同じ #----------------- # col1 col2 # 0 False False # 1 False False # 2 True True #----------------- np.logical_and(DA, DB) # DA & DB と同じ #----------------- # col1 col2 # 0 True True # 1 False False # 2 False False #----------------- np.logical_or(DA, DB) # DA | DB と同じ #----------------- # col1 col2 # 0 True True # 1 True True # 2 False False #----------------- np.logical_xor(DA, DB) # DA ^ DB と同じ #----------------- # col1 col2 # 0 False False # 1 True True # 2 False False #----------------- |