如何使用计算值在数据框中添加另一列

时间:2017-12-13 04:37:05

标签: python pandas dataframe sentiment-analysis text-analysis

我有一个新闻数据集,我正在携带NLP。 我现在有2个函数,一个计算相似度,另一个计算情绪,它们都从数据帧中获取输入,我试图做的是在数据帧中创建另一个列,其中包含相似性和计算值。情绪(正/负)

功能如下

i=0
for i in range(0, 9):
    text1 = df.description[i]
    text2 = df.title[i]


    vector1 = similarity.text_to_vector(text1)
    vector2 = similarity.text_to_vector(text2)

    token1 = similarity.tokenize(text1)
    token2 = similarity.tokenize(text2)

    jaccard = similarity.jaccard_similarity(token1,token2)
    print ('Jaccard Similarity:', jaccard)

    i=i+1

输出:

('Jaccard Similarity:', 0.07142857142857142)
('Jaccard Similarity:', 0.125)
('Jaccard Similarity:', 0.03225806451612903)
('Jaccard Similarity:', 0.07692307692307693)
('Jaccard Similarity:', 0.2)
('Jaccard Similarity:', 0.07407407407407407)
('Jaccard Similarity:', 0.12)
('Jaccard Similarity:', 0.043478260869565216)
('Jaccard Similarity:', 0.0)

代码:

i=0
for i in range(0, 9):
    blob = TextBlob(df.description[i], analyzer=NaiveBayesAnalyzer())
    y = blob.sentiment.classification
    print ('Result', y)
    i=i+1

输出:

('Result', 'pos')
('Result', 'neg')
('Result', 'pos')
('Result', 'pos')
('Result', 'pos')
('Result', 'neg')
('Result', 'pos')
('Result', 'pos')
('Result', 'neg')

1 个答案:

答案 0 :(得分:1)

这将解决您的问题

def jaccard(text1,text2):
    vector1 = similarity.text_to_vector(text1)
    vector2 = similarity.text_to_vector(text2)

    token1 = similarity.tokenize(text1)
    token2 = similarity.tokenize(text2)

    jaccard = similarity.jaccard_similarity(token1,token2)
    return jaccard

def result(t1):
    blob = TextBlob(t1, analyzer=NaiveBayesAnalyzer())
    y = blob.sentiment.classification
    return y

df['result'] = df['description'].map(lambda x: result(x))

df['jaccard'] = df.apply(lambda x: lambda x : jaccard(x['description'],x['title']))