Question
How can I perform bitwise operations in Pandas?
How &
works on integers
On integers the &
operator performs a bitwise mask
>>> mask = 0b1100 # 4 and 8 bits on
>>> 7 & mask
4
How &
works in Pandas
Is there some way to perform bitwise masking operations in Pandas? The &
operator does something else.
>>> 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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…