使一个依赖于另外两个布尔列的熊猫布尔列
问题描述:
我的数据框中已经有两个布尔列,low
和high
。然后我想要定义一个in_range
列,它反映了一行中的low
和high
列均为false的情况。下面是我的尝试:使一个依赖于另外两个布尔列的熊猫布尔列
df['in_range'] = not (df.low or df.high)
这会产生错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我想这样做在逐行基础。我尝试添加.bool()
导致这个错误:
ValueError: can only convert an array of size 1 to a Python scalar
什么是错我的代码,什么是基于现有的布尔列的新布尔列的正确方法?
答
使用位语法:
~(df.low | df.high)
Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses.
使用布尔矢量指数系列作品完全一样的numpy的ndarray: