python - Pandas 中返回数字而不是书籍的按位运算?

标签 python pandas bit-manipulation

问题

如何在 Pandas 中执行按位运算?

& 如何处理整数

& 运算符对整数执行按位掩码

>>> mask = 0b1100  # 4 and 8 bits on
>>> 7 & mask
4

& 在 Pandas 中的工作原理

有什么方法可以在 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

最佳答案

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

它甚至返回一个系列——非常酷!

关于python - Pandas 中返回数字而不是书籍的按位运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22231438/

相关文章:

python - 无法让我的 Sprite 沿着路径点移动

Python 正则表达式 : return the whole sentence with a certain word in it from period to period

Python:如果所选列为空,则从 Pandas Dataframe 中删除行

assembly - 使用按位运算将 Int 转换为 Float 或将 Float 转换为 Int(软件浮点)

c - C 中的位运算和掩码

python - 为什么我会收到此错误以及如何修复它?

python - 有效地将 numpy 结构化数组转换为二维数组

python - 如何从列中提取字符串的某些部分以在 Pandas 中创建其他列

python - 将以下 xml 元素转换为 pandas 数据框时出现问题吗?

mysql - 如何使用快速二进制操作在 MySQL 中操作 SET 数据类型?