首页 > 解决方案 > 循环检索 pandas.core.series.Series 中的情绪分析

问题描述

我有 47 篇新闻文章要从中提取情绪。它们是 JSON 格式(文章的日期、标题和正文)。我想要的只是使用 TextBlob 获取带有情绪的列表。到目前为止,我正在执行以下操作:

import json
import pandas
from textblob import TextBlob

appended_data = []

for i in range(1,47):
    df0 = pandas.DataFrame([json.loads(l) for l in open('News_%d.json' % i)])
    appended_data.append(df0)


appended_data = pandas.concat(appended_data)

doc_set = appended_data.body
docs_TextBlob = TextBlob(doc_set)


for i in docs_TextBlob:
    print(docs_TextBlob.sentiment)

Obvioulsy,我收到以下错误:TypeError: The text argument passed to __init__(text) must be a string, not <class 'pandas.core.series.Series'>关于如何使用情绪度量创建列表的任何想法?

标签: pythonsentiment-analysistextblob

解决方案


DataFrame要在情绪中创建一个新列:

appended_data['sentiment'] = appended_data.body.apply(lambda body: TextBlob(body).sentiment)

推荐阅读