我想根据python-datatable语法中另一列中的正则表达式匹配为新列赋值。
通过正则表达式对行进行DTget,为新列赋值,
import pandas as pd
import datatable as dt
from datatable import f, Frame
import re as re
DT = dt.Frame({'a' : [1,2,3,4], 'b' : ['hi', 'foo', 'fat', 'cat']})
DT['new_col']=DT[:,f.b]
DT['new_col'] = Frame([re.sub('f.*','words starting with f', s) for s in DT[:, "new_col"].to_list()[0]])
DT.head()
DT['new_col'] = Frame([re.sub('c.*','words starting with c', s) for s in DT[:, "new_col"].to_list()[0]])
DT.head()有没有其他的解决方案,不需要在datatable包中使用"to_list()“或者更多的东西来进行转换(没有循环)?
在这里,这个问题中的正则表达式的结果不允许对整个列进行操作:Python data.table row filter by regex这是针对熊猫的,但不是针对datatable:How to filter rows in pandas by regex的。
发布于 2020-06-19 17:22:18
我认为现在你可以选择解决方案。所需的实现将在datatable成长过程中被查找并添加到datatable中。
导入库
import pandas as pd
import datatable as dt
from datatable import f,by
import re as re创建DT
DT_X = dt.Frame({'a' : [1,2,3,4], 'b' : ['hi', 'foo', 'fat', 'cat']})并进行必要的操作,如下所示
DT_X[:,f[:].extend({'new_col':dt.Frame([re.sub('f.*','words starting with f', s) for s in DT_X[:, f.b].to_list()[0]])})]输出:
| a b new_col
-- + -- --- ---------------------
0 | 1 hi hi
1 | 2 foo words starting with f
2 | 3 fat words starting with f
3 | 4 cat cathttps://stackoverflow.com/questions/62388142
复制相似问题