【问题标题】:spacy stemming on pandas df column not working大熊猫df列上的spacy词干不起作用
【发布时间】:2020-06-15 09:53:22
【问题描述】:

如何在 Pandas Dataframe 列上应用词干提取

我正在使用这个函数进行词干提取,这对字符串非常有效

xx='kenichan dived times ball managed save 50 rest'

def make_to_base(x):
    x_list = []
    doc = nlp(x)
    for token in doc:
        lemma=str(token.lemma_)
        if lemma=='-PRON-' or lemma=='be':
            lemma=token.text
        x_list.append(lemma)
    print(" ".join(x_list))    
make_to_base(xx)

但是当我在我的 pandas 数据框列上应用这个函数时,它既不工作也不给出任何错误

x = list(df['text']) #my df column
x = str(x)#converting into string otherwise it is giving error
make_to_base(x)

我尝试了不同的方法,但没有任何效果。像这样

df["texts"] =  df.text.apply(lambda x: make_to_base(x))

make_to_base(df['text'])

我的数据集如下所示:

df['text'].head()
Out[17]: 
0    Hope you are having a good week. Just checking in
1                              K..give back my thanks.
2          Am also doing in cbe only. But have to pay.
3    complimentary 4 STAR Ibiza Holiday or £10,000 ...
4    okmail: Dear Dave this is your final notice to...
Name: text, dtype: object

【问题讨论】:

  • 试试df['texts'] = df['text'].apply(lambda x: make_to_base(str(x)))
  • 在 df['texts'] 中仅获取“无”值
  • 然后发布一个示例数据框。
  • 更新问题请查收
  • 用return " ".join(x_list)替换print(" ".join(x_list))

标签: python pandas nlp spacy


【解决方案1】:

你需要实际返回你在make_to_base方法中得到的值,使用

def make_to_base(x):
    x_list = []
    for token in nlp(x):
        lemma=str(token.lemma_)
        if lemma=='-PRON-' or lemma=='be':
            lemma=token.text
        x_list.append(lemma)
    return " ".join(x_list)

然后,使用

df['texts'] =  df['text'].apply(lambda x: make_to_base(x))

【讨论】:

    猜你喜欢
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2013-07-29
    • 2015-10-11
    • 2017-12-29
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多