Textblob sentiment analysis on a csv file

15,565

Solution 1

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

Solution 2

You need to iterate over each row in the csv file.

First, open the csv file.

Then for each row in the file, we can access the first column in the row with row[0].

import csv
from textblob import TextBlob

infile = '/path/to/file.csv'

with open(infile, 'r') as csvfile:
    rows = csv.reader(csvfile)
    for row in rows:
        sentence = row[0]
        blob = TextBlob(sentence)
        print sentence
        print blob.sentiment.polarity, blob.sentiment.subjectivity
Share:
15,565
u.1234
Author by

u.1234

Updated on June 07, 2022

Comments

  • u.1234
    u.1234 almost 2 years

    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
    
  • u.1234
    u.1234 about 8 years
    i edited your answer and it now works! thanks a lot!
  • chishaku
    chishaku about 8 years
    @u.1234 Glad to hear. As it seems you are new to Stack Overflow, I'd like to say welcome. If you find a solution that works for you and you'd like to share it to benefit others, it's best to edit your original question and add the solution there. For any answer that is helpful or satisfies your question, you can upvote and/or "accept" the answer. Learn more on how to make the most of Stack Overflow here: stackoverflow.com/tour
  • ykombinator
    ykombinator almost 7 years
    from textblob import TextBlob gives me a ModuleNotFoundError: No module named 'textblob'
  • rohit keshav
    rohit keshav about 6 years
    ^ pip install textblob ; refer - textblob.readthedocs.io/en/dev/index.html
  • Chedi Bechikh
    Chedi Bechikh almost 6 years
    @u.1234 I don't understand the attribute sentiment.polarity is taken from where? we know that we open a csv file, so it may contain 3 column by row