2015-08-26 3 views
4

Я знаю, что логический AND is &, и логический ИЛИ есть | в серии Pandas, но я искал логический XOR, основанный на элементах. Я мог бы выразить это с точки зрения AND и OR, я полагаю, но я бы предпочел использовать XOR, если он доступен.Element-wise XOR in pandas

Спасибо!

+1

Ьгу '^ '.......? –

ответ

5

Python исключающее: a^b

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

Тестирование производительности - результат равны:

1. Последовательность случайных Булев с размером 10000

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 . Последовательность случайного boo наклоняется с размером 1000

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

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

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