Skip to main content
 首页 » 编程设计

Python data.table 行过滤正则表达式

2024年09月03日30emanlee

Python 的 data.table 相当于 %like% 是什么?

简短示例:

dt_foo_bar = dt.Frame({"n": [1, 3], "s": ["foo", "bar"]})   
dt_foo_bar[re.match("foo",f.s),:] #works to filter by "foo" 

我曾期望这样的东西能起作用:

dt_foo_bar[re.match("fo",f.s),:]  

但它返回“预期的字符串或类似字节的对象”。 我很想开始在 Python 中使用新的 data.tables 包,就像我在 R 中使用它一样,但我处理的文本数据比数字数据多得多。

提前致谢。

请您参考如下方法:

从 0.9.0 版本开始,datatable 包含函数 .re_match() 执行正则表达式过滤。例如:

>>> import datatable as dt 
>>> dt_foo_bar = dt.Frame(N=[1, 3, 5], S=["foo", "bar", "fox"]) 
>>> dt_foo_bar[dt.f.S.re_match("fo."), :] 
     N  S   
--  --  --- 
 0   1  foo 
 1   5  fox 
 
[2 rows x 2 columns] 

通常,.re_match() 应用于列表达式并生成一个新的 bool 列,指示每个值是否与给定的正则表达式匹配。