问题
What is the data.table for python equivalent of %like%?
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.
Thanks in advance.
回答1:
Since version 0.9.0, datatable contains function .re_match()
which performs regular expression filtering. 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.
来源:https://stackoverflow.com/questions/54621252/python-data-table-row-filter-by-regex