问题描述
我无法使用预定义的项目列表执行标准的 in
操作.我想做这样的事情:
I am unable to perform a standard in
operation with a pre-defined list of items. I am looking to do something like this:
# Construct a simple example frame
from datatable import *
df = Frame(V1=['A','B','C','D'], V2=[1,2,3,4])
# Filter frame to a list of items (THIS DOES NOT WORK)
items = ['A','B']
df[f.V1 in items,:]
这个例子导致错误:
TypeError:布尔值不能用作行选择器
不幸的是,in
操作似乎没有内置对象.我想使用类似于 R 语言原生的 %in%
运算符.有没有什么方法可以在python中实现这一点?
Unfortunately, there doesn't appear to be a built-in object for in
operations. I would like to use something like the %in%
operator that is native to the R language. Is there any method for accomplishing this in python?
我可以通过使用多个等于"运算符来采用这种方法,但是当您要考虑大量项目时,这很不方便:
I can take this approach with the use of multiple 'equals' operators, but this is inconvenient when you want to consider a large number of items:
df[(f.V1 == 'A') | (f.V1 == 'B'),:]
数据表 0.10.1
蟒蛇 3.6
datatable 0.10.1
python 3.6
推荐答案
你也可以试试这个:
首先导入所有必要的包,
First import all the necessary packages as,
import datatable as dt
from datatable import by,f,count
import functools
import operator
创建示例数据表:
DT = dt.Frame(V1=['A','B','C','D','E','B','A'], V2=[1,2,3,4,5,6,7])
列出要在观察结果中过滤的值,在您的情况下是
Make a list of values to be filtered among the observations, in your case it is
sel_obs = ['A','B']
现在使用 funtools 和 operators 模块创建过滤器表达式,
Now create a filter expression using funtools and operators modules,
filter_rows = functools.reduce(operator.or_,(f.V1==obs for obs in sel_obs))
最后在数据表上应用上面创建的过滤器
Finally apply the above created filter on datatable
DT[fil_rows,:]
它的输出为-
Out[6]:
| V1 V2
-- + -- --
0 | A 1
1 | B 2
2 | B 6
3 | A 7
[4 rows x 2 columns]
您可以使用运算符来进行不同类型的过滤.
You can just play around with operators to do different type of filterings.
@sammyweemy 的解决方案也应该有效.
@sammyweemy's solution should also work.
这篇关于py-datatable 'in' 运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!