gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:19 25 4
gpt4 key购买 nike

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 列,指示每个值是否与给定的正则表达式匹配。

关于Python data.table 行过滤正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54621252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com