0

I'm trying to apply the textacy.extract.subject_verb_object_triples function to a pandas df column. The function returns empty generator objects, instead of subject_verb_object_triples when applied like so:

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

or

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

I've also tried:

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)

How can I implement the function on my dataframe column to return the correct function output?

W.R
  • 187
  • 1
  • 1
  • 14
  • You are not returning anything from your defined function. You need to add a return statement to get assignments backt to your columns with apply. – Scott Boston Feb 04 '19 at 17:17

1 Answers1

0

The reason is that textacy.extract.subject_verb_object_triples returns a generator, which must be converted into some kind of iterable. Both your approaches are workable, but need some modification.

The first way: consume the generators

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

The second way: write a separate function to apply

def extract_SVO1(text):
    new_doc = textacy.extract.subject_verb_object_triples(text)
    new_list = list(new_doc)
    return new_list
gmds
  • 19,325
  • 4
  • 32
  • 58