我正在尝试将 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
我认为简短的答案是否定的(
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]))]