MRo*_*lin 5 python bit-manipulation pandas
如何在Pandas中执行按位运算?
&处理整数在整数上,&运算符执行按位掩码
>>> mask = 0b1100 # 4 and 8 bits on
>>> 7 & mask
4
Run Code Online (Sandbox Code Playgroud)
&在熊猫中运作有没有办法在Pandas中执行按位屏蔽操作?该&运营商做别的事情.
>>> df = DataFrame([1, 2, 3, 4, 5, 6, 7, 8], columns=['data'])
>>> df.data & mask
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 True
Name: data, dtype: bool
Run Code Online (Sandbox Code Playgroud)
In [184]: df = pd.DataFrame([1, 2, 3, 4, 5, 6, 7, 8], columns=['data'])
In [185]: mask = 0b1100
In [186]: np.bitwise_and(df['data'], mask)
Out[186]:
0 0
1 0
2 0
3 4
4 4
5 4
6 4
7 8
Name: data, dtype: int64
Run Code Online (Sandbox Code Playgroud)
它甚至回归系列 - 非常酷!