简体   繁体   English

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

[英]Python data.table row filter by regex

What is the data.table for python equivalent of %like%?相当于 %like% 的 python 的 data.table 是什么?

Short example:简短示例:

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

I had expected something like this to work:我原以为这样的事情会起作用:

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

But it returns "expected string or bytes-like object".但它返回“预期的字符串或类似字节的对象”。 I'd love to start using the new data.tables package in Python the way I use it in R but I work a lot more with text data than numeric.我很想开始在 Python 中使用新的 data.tables 包,就像我在 R 中使用它的方式一样,但我更多地处理文本数据而不是数字数据。

Thanks in advance.提前致谢。

Since version 0.9.0, datatable contains function .re_match() which performs regular expression filtering.从 0.9.0 版本开始,数据表包含执行正则表达式过滤的函数.re_match() For example:例如:

>>> 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]

In general, .re_match() applies to a column expression and produces a new boolean column indicating whether each value matches the given regular expression or not.通常, .re_match()应用于列表达式并生成一个新的布尔列,指示每个值是否与给定的正则表达式匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM