我在Pandas中使用布尔索引.问题是为什么声明:
a[(a['some_column']==some_number) & (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
工作正常,而
a[(a['some_column']==some_number) and (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
存在错误?
例:
a=pd.DataFrame({'x':[1,1],'y':[10,20]})
In: a[(a['x']==1)&(a['y']==10)]
Out: x y
0 1 10
In: a[(a['x']==1) and (a['y']==10)]
Out: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
x = 1 # 0001
x << 2 # Shift left 2 bits: 0100
# Result: 4
x | 2 # Bitwise OR: 0011
# Result: 3
x & 1 # Bitwise AND: 0001
# Result: 1
Run Code Online (Sandbox Code Playgroud)
我可以理解Python(和其他语言)中的算术运算符,但我从来没有完全理解'按位'运算符.在上面的例子中(来自Python书),我理解左移但不是其他两个.
另外,实际使用的是按位运算符?我很欣赏一些例子.
在进行布尔数组比较时,使用&代替*或|替代是否有任何优势/惯例+?这些总是相同吗?
(如果这些是在文档中,链接可能是一个可接受的答案,但我天真地搜索'numpy&符'和'numpy elementwise布尔比较'没有产生任何相关的东西)
>>> 5 > 4 & 6 > 5
Run Code Online (Sandbox Code Playgroud)
为什么上面的表达式False在 Python 中给出,if 5 > 4isTrue和6 > 5is also True?