有没有办法在 python data.table 中使用正则表达式(除了 re.match)?

问题描述 投票:0回答:1

我正在尝试将 py data.table 中的列转换为整数。该列包含空格和其他不需要的字符,删除这些字符后,可以将其转换为整数。我无法在 py data.table 中完成此任务(而我可以在 Polars、Python、R data.table 中完成此任务):

# remove .00 if exists, minus after number etc.
df[:, update(weird_col = re.sub(r"\.[0-9]{0,2}|-", "", df[:, 'weird_col']))] 

TypeError: expected string or bytes-like object

df[:, update(weird_col = re.sub(r"\.[0-9]{0,2}|-", "", df['weird_col']))]
TypeError: cannot use a string pattern on a bytes-like object
python regex data.table
1个回答
0
投票

我认为简短的答案是否定的(

re.match()
dt.re
模块中的唯一功能)。

但是,你可以这样做:

from datatable import Frame, update
import re

wc = df["weird_col"].to_list()[0]
regex = r"\.[0-9]{0,2}|-"

df[:, update(weird_col = Frame([re.sub(regex, "", i) for i in wc]))]


© www.soinside.com 2019 - 2024. All rights reserved.