pandas 熊猫:两个布尔系列的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25291753/
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:22:11  来源:igfitidea点击:

Pandas: Sum of two boolean series

pythonpandas

提问by exp1orer

In Python:

在 Python 中:

In [1]: True+True
Out[1]: 2

So after the following set-up:

所以在以下设置之后:

import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False])

What I want is to find the element-wise sum of ser1and ser2, with the booleans treated as integers for addition as in the Python example.

我想要的是找到ser1and的元素总和ser2,将布尔值视为整数进行加法,如 Python 示例中所示。

But Pandas treats the addition as an element-wise "or" operator, and gives the following (undesired) output:

但 Pandas 将加法视为逐元素的“或”运算符,并给出以下(不需要的)输出:

In [5]: ser1+ser2
*/lib/python2.7/site-packages/pandas/computation/expressions.py:184: UserWarning: evaluating in Python space because the '+' operator is not supported by numexpr for the bool dtype, use '|' instead
  unsupported[op_str]))
Out[5]: 
0     True
1     True
2     True
3    False
dtype: bool

I know I get can get my desiredoutput using astype(int)on either series:

我知道我可以在任一系列上使用我想要的输出astype(int)

In [6]: ser1.astype(int) + ser2
Out[6]: 
0    2
1    1
2    1
3    0
dtype: int64

Is there another (more "pandonic") way to get the [2,1,1,0] series? Is there a good explanation for why simple Series addition doesn't work here?

是否有另一种(更“狂热”)的方法来获得 [2,1,1,0] 系列?为什么简单的系列加法在这里不起作用有一个很好的解释吗?

采纳答案by DSM

IIUC, what you're looking for is that the operative convention is that of numpy bool arrays, not Python bools:

IIUC,您要寻找的是操作约定是 numpy bool 数组的约定,而不是 Python bool 的约定:

>>> a = True
>>> a+a
2
>>> import numpy as np
>>> np.array([a])
array([ True], dtype=bool)
>>> np.array([a]) + np.array([a])
array([ True], dtype=bool)

Could've gone either way, and if memory serves at least one pandas dev was surprised by this behaviour, but doing it this way matches the idea that Series are typed.

可以采用任何一种方式,如果内存至少有一个 Pandas 开发人员对这种行为感到惊讶,但这样做符合 Series 被键入的想法。

回答by Charles Clayton

Instead of +use &

而不是+使用&

import pandas as pd
ser1 = pd.Series([True,True,False,False])
ser2 = pd.Series([True,False,True,False]) 

print(ser1 & ser2) 

>> 0     True
>> 1    False
>> 2    False
>> 3    False
>> dtype: bool