【问题标题】:Python data.table row filter by regex正则表达式的 Python data.table 行过滤器
【发布时间】:2019-07-04 09:33:13
【问题描述】:

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

简短示例:

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 中使用它一样,但我更多地使用文本数据而不是数字。

提前致谢。

【问题讨论】:

    标签: python py-datatable


    【解决方案1】:

    从 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() 应用于列表达式并生成一个新的布尔列,指示每个值是否与给定的正则表达式匹配。

    【讨论】:

    • 我在文档中找不到此功能或任何与字符串相关的数据处理。
    • 你还知道如何根据正则表达式生成新列吗?我目前使用它,但它看起来不是 to_list 转换的最佳方法: DT['new_name'] = Frame([re.sub('some_regex_pattern','value_for_new_column', s) for s in DT [:, "column_for_regex"].to_list()[0]])
    • @Zappageck 恐怕现在不可能
    猜你喜欢
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2016-09-02
    • 2012-12-10
    相关资源
    最近更新 更多