python - Pandas 中的逐元素异或

标签 python pandas logic xor

我知道逻辑与是&,逻辑或是|在 Pandas 系列中,但我一直在寻找一个元素明智的逻辑异或。我想我可以用 AND 和 OR 来表达它,但如果可用,我更愿意使用 XOR。

谢谢!

最佳答案

Python 异或:a ^ b

Numpy logical XOR : np.logical_xor(a,b)

测试性能 - 结果相同:

<强>1。大小为 10000 的随机 bool 值序列

In [7]: a = np.random.choice([True, False], size=10000)
In [8]: b = np.random.choice([True, False], size=10000)

In [9]: %timeit a ^ b
The slowest run took 7.61 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 11 us per loop

In [10]: %timeit np.logical_xor(a,b)
The slowest run took 6.25 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 11 us per loop

<强>2。大小为 1000 的随机 bool 值序列

In [11]: a = np.random.choice([True, False], size=1000)
In [12]: b = np.random.choice([True, False], size=1000)

In [13]: %timeit a ^ b
The slowest run took 21.52 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.58 us per loop

In [14]: %timeit np.logical_xor(a,b)
The slowest run took 19.45 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.58 us per loop

<强>3。大小为 100 的随机 bool 值序列

In [15]: a = np.random.choice([True, False], size=100)
In [16]: b = np.random.choice([True, False], size=100)

In [17]: %timeit a ^ b
The slowest run took 33.43 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 614 ns per loop

In [18]: %timeit np.logical_xor(a,b)
The slowest run took 45.49 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 616 ns per loop

<强>4。大小为 10 的随机 bool 值序列

In [19]: a = np.random.choice([True, False], size=10)
In [20]: b = np.random.choice([True, False], size=10)

In [21]: %timeit a ^ b
The slowest run took 86.10 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 509 ns per loop

In [22]: %timeit np.logical_xor(a,b)
The slowest run took 40.94 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 511 ns per loop

关于python - Pandas 中的逐元素异或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237794/

相关文章:

c# - 如何为我的坐标系获取 "thinner"图?

python - 加快处理庞大数据集的 Python 文件

Python AST 代码示例取自 Serious Python : Black-Belt Advice on Deployment, 可扩展性、测试等一书

Python Pandas - 将两列按不同方向分组

vba - 编写转换表算法最有效的方法是什么

python - 将具有相同 ID 的多行(具有一些非字符串值)合并到 pandas 中的一个分隔行中

python - 如何在 python 中计算正交向量?

python - 如何列出类函数内的变量或属性?

python - Pandas 按位置覆盖列名

python - 将 Pandas 导出为 CSV 会导致 CSV 中出现#NAME 错误