Easily resolved (&, |) and the difference between (and, or)

(&, |), And (and, or) are similar between the two groups of operators, used in a "and" / "or" upper, in use slightly different.
(&, |), And (and, or) are used to compare two sets of variables, the format is essentially:

a & b
a | b
a and b
a or b

If a, b is a number variable, &, | indicates a bit operation, and, or non-zero depending on whether the output is determined,

  1. &, |:

    	```
    	# 1&2,2在二进制里面是10,1在二进制中是01,那么01与运算10得到是0 
    
    	1 & 2         # 输出为 0, 
    	1 | 2          # 输出为3
    	```
    
  2. and, or:

     # 判断变量是否为0, 是0则为False,非0判断为True,
     # and中含0,返回0; 均为非0时,返回后一个值, 
    2 and 0   # 返回0
    2 and 1   # 返回1
    1 and 2   # 返回2
    
    # or中, 至少有一个非0时,返回第一个非0,
    2 or 0   # 返回2
    2 or 1   # 返回2
    0 or 1   # 返回1 
    

How a, b are logical variables, the use of two types of basically the same

	```
	In[103]:(3>0) | (3<1)
	Out[103]: True
	In[104]:(3>0) and (3<1)
	Out[104]: False
	In[105]:(3>0) or (3<1)
	Out[105]: True
	In[106]:(3>0) & (3<1)
	Out[106]: False
	```

It is worth mentioning during the slicing process DataFrame, pay attention to the use of a logical variable,
you need to be found to meet the logical condition when a plurality of data, and to use the & |, under certain conditions with and / or being given 'ValueError: The truth value of a Series is ambiguous

target_url = "http://aima.cs.berkeley.edu/data/iris.csv"
data = pd.read_csv(target_url, header=None, columns=['s_line', 's_wid', 'p_line', 'p_wid', 'kind'])
data.columns = ['s_line', 's_wid', 'p_line', 'p_wid', 'kind']
x_data = data.iloc[:, :-1]

# 在多个逻辑条件下,用& 或者|,
x_1 = x_data[x_data['s_line'] > 6 & x_data['p_wid'] > 0]

Guess you like

Origin blog.csdn.net/weixin_42625143/article/details/94616595