pandas 两个 Series 对象的布尔比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24535794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:12:53  来源:igfitidea点击:

Boolean comparison of two Series objects

pythonpandas

提问by veor

I have two Series which have a format equal to this:

我有两个系列,它们的格式与此相同:

0    False
1    False
2    False
3    True
4    True
Name: foo, dtype: bool

0    True
1    False
2    False
3    True
4    True
Name: bar, dtype: bool

I want to create a new Series with the resulting boolean comparison from these. Something like this:

我想用这些结果的布尔比较创建一个新系列。像这样的东西:

result = foo and bar
>>> print result
0    False
1    False
2    False
3    True
4    True
Name: result, dtype: bool

Using the obvious result = foo and barsimply results in the following error:

使用明显的result = foo and bar只会导致以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I looked at those functions, but neither seem to do what I wish.

我查看了这些功能,但似乎都没有按照我的意愿行事。

How can I do an element-to-element boolean comparison of a Series resulting in a new Series?

如何对系列进行元素到元素的布尔比较,从而产生新系列?

回答by chrisb

You need to use the bitwise and operator &.

您需要使用按位和运算符&

result = foo & bar