【发布时间】:2021-07-16 21:09:50
【问题描述】:
我将以下函数传递给具有 300 万个 cmets 的 pandas 列,以提取形容词。我期待它可以很快完成,因为它可以在并行计算中完成。虽然它需要大约 5 个小时左右,就好像它是一个 for 循环一样。有没有可能解决这个问题?喜欢 Cython?
def get_adjectives(row):
clean_row=''
if type(row)==str:
for word in row.split():
if nltk.pos_tag([word])[0][1] in ['JJ','JJR','JJS']:
clean_row=clean_row+word+' '
return clean_row
df['adjectives'] = df[text_column].apply(get_adjectives)
【问题讨论】:
-
以你的方式添加 n 个字符串是 O(n*n)。首先将它们放入列表并在最后加入 => O(n)。不需要 cython。
-
apply必须每行运行一次get_adjectives,即 300 万次。
标签: pandas lambda nltk apply cython