Textblob sentiment analysis on a csv file

u.1234 picture u.1234 · Feb 22, 2016 · Viewed 14.5k times · Source

I have a csv file with around 50 rows of sentences. I'm using the textblob sentiment analysis tool. To test the polarity of a sentence, the example shows you write a sentence and the polarity and subjectivity is shown. However, it only works on a single sentence, I want it to work for the csv file that I have, as I can't put in each row and test them individually as it would take too long. How would I go about doing this?

TextBlob show this example, when I type in a sentence, the polarity shows, you can't input two sentences at one time, it doesn't let you. How would i input my csv file into the example below to give me the polarity for all rows?

>>> testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
>>> testimonial.sentiment
Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)
>>> testimonial.sentiment.polarity
0.39166666666666666

edited chishaku solution and it worked for me. Solution:

import csv
from textblob import TextBlob

infile = 'xxx.csv'

with open(infile, 'r') as csvfile:
    rows = csv.reader(csvfile)
    for row in rows:
        sentence = row[0]
        blob = TextBlob(sentence)
        print blob.sentiment

Answer

Flavian Hautbois picture Flavian Hautbois · Feb 22, 2016

I really like pandas when it comes to processing CSVs, even though this a kind of too generic for what you want to achieve. But maybe you'll want to do more processing with your data so I'm posting the pandas solution.

import pandas as pd

# To read a CSV file
# df = pd.read_csv('sentences.csv')
df = pd.DataFrame({'sentence': ['I am very happy', 'I am very sad', 'I am sad but I am happy too']})

from textblob import TextBlob

# The x in the lambda function is a row (because I set axis=1)
# Apply iterates the function accross the dataframe's rows
df['polarity'] = df.apply(lambda x: TextBlob(x['sentence']).sentiment.polarity, axis=1)
df['subjectivity'] = df.apply(lambda x: TextBlob(x['sentence']).sentiment.subjectivity, axis=1)

>>> print(df)
                      sentence  polarity  subjectivity
0              I am very happy      1.00             1
1                I am very sad     -0.65             1
2  I am sad but I am happy too      0.15             1