【问题标题】:How to implement function on pandas dataframe column如何在熊猫数据框列上实现功能
【发布时间】:2019-02-04 16:25:28
【问题描述】:

我正在尝试将 textacy.extract.subject_verb_object_triples 函数应用于 pandas df 列。该函数返回空的生成器对象,而不是像这样应用时的 subject_verb_object_triples:

sp500news3['title'].apply(lambda x: textacy.extract.subject_verb_object_triples)

sp500news3['title'].apply(textacy.extract.subject_verb_object_triples)

我也试过了:

import spacy
import textacy
def extract_SVO1(text):
    new_doc = textacy.extract.subject_verb_object_triples(text)
    new_list = list(new_doc)
    text = new_list

sp500news3['title'] = sp500news3['title'].apply(extract_SVO1)

如何在我的数据框列上实现函数以返回正确的函数输出?

【问题讨论】:

  • 你没有从你定义的函数中返回任何东西。您需要添加一个 return 语句以使用 apply 将分配返回到您的列。

标签: python pandas nlp spacy textacy


【解决方案1】:

原因是textacy.extract.subject_verb_object_triples 返回了一个生成器,它必须被转换成某种可迭代的。您的两种方法都可行,但需要进行一些修改。

第一种方式:消费生成器

sp500news3['title'].apply(lambda x: textacy.extract.subject_verb_object_triples).apply(pd.Series)

第二种方式:写一个单独的函数来申请

def extract_SVO1(text):
    new_doc = textacy.extract.subject_verb_object_triples(text)
    new_list = list(new_doc)
    return new_list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2017-04-03
    • 2022-11-13
    相关资源
    最近更新 更多