在 pandas 中按位运算返回数字而不是布尔值? [英] Bitwise operations in Pandas that return numbers rather than bools?

查看:59
本文介绍了在 pandas 中按位运算返回数字而不是布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在熊猫中执行按位运算?

How can I perform bitwise operations in Pandas?

在整数上,&运算符执行按位掩码

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

推荐答案

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

它甚至返回一个系列-太酷了!

It even returns a Series -- pretty cool!

这篇关于在 pandas 中按位运算返回数字而不是布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆