Search code examples
pythonsentiment-analysistextblob

Loop to retrieve sentiment analysis in pandas.core.series.Series


I have 47 news-articles that I want to extract the sentiment from. They are JSON format (Date, title and body of the article). All I want is to obtain a list with the sentiment using TextBlob. So far I am doing the following:

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, I get the following error: TypeError: The text argument passed to __init__(text) must be a string, not <class 'pandas.core.series.Series'> Any idea on how to create a list with the sentiment measure?


Solution

  • To create a new column in the DataFrame with the sentiment:

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