如何在熊猫df中将列表函数应用于textacy生成器obj

时间:2019-02-04 14:00:53

标签: python pandas nlp spacy textacy

我正在将“ list”功能应用于包含生成器对象的熊猫col,以尝试显示col中的所有生成器对象。申请时,col返回空列表。 'subject_verb_object_triples'是文本功能(https://chartbeat-labs.github.io/textacy/_modules/textacy/extract.html

打印(sp500news3)

date_publish    title
79944   2007-01-29 19:08:35 <generator object subject_verb_object_triples at 0x1a42713550>
181781  2007-12-14 19:39:06 <generator object subject_verb_object_triples at 0x1a42713410>
213175  2008-01-22 11:17:19 <generator object subject_verb_object_triples at 0x1a427135f0>
93554   2008-01-22 18:52:56 <generator object subject_verb_object_triples at 0x1a427135a0>

In []: sp500news3["title"].apply(list)
Out []: 79944     []
        181781    []
        213175    [] ...

期望的输出是元组,例如:

[(Sky proposal, is, matter), (Sky proposal, is, Mays spokesman)], 
[(Women, lag, Intel report)], 
[(Amazon, expected, to unveil)], 
[(Goldman Sachs, raising, billion)], 
[(MHP, opens, books)], 
[(Disney, hurls, magic), (Disney, hurls, moolah)], 
[(Amazon, offering, loans), (Amazon, offering, to)], ....

如何在数据框中显示预期的输出?

1 个答案:

答案 0 :(得分:0)

我已经测试了以下代码,并且工作正常

import textacy
import pandas as pd
from textacy import preprocessing
pd.options.display.max_colwidth=-1
df['<New Column name'>]=df['<Your column name that needs to be processed>'].apply(lambda x:preprocessing.normalize_whitespace(preprocessing.remove_punctuation(str(x))))