0
import pandas as pd
import csv
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

with open('Before.csv', "r", errors='ignore') as f:
    reader = csv.reader(f)
    your_list = list(reader)

analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(alist):
    for aSentence in alist: 
      aSnt = analyser.polarity_scores(aSentence[0])
      print(str(aSnt))

print_sentiment_scores(your_list)

My output reprex is:

{'neg': 0.0, 'neu': 0.492, 'pos': 0.508, 'compound': 0.4754}
{'neg': 0.0, 'neu': 0.367, 'pos': 0.633, 'compound': 0.7845}
{'neg': 0.0, 'neu': 0.691, 'pos': 0.309, 'compound': 0.8004}
{'neg': 0.0, 'neu': 0.462, 'pos': 0.538, 'compound': 0.5413}
{'neg': 0.0, 'neu': 0.636, 'pos': 0.364, 'compound': 0.7906}

so I did df_before = print_sentiment_scores(your_list)

and then df_before.to_csv("df_Before_sentiment.csv")

But I received an error AttributeError: 'NoneType' object has no attribute 'to_csv'. How can I convert my ouput of print_sentiment_scores(your_list) to a csv in a dataframe format so all values show under each header like neg,neu,pos,compound?

  • `print_sentiment_scores` does not return anything and it's `None`. So, you can't call `to_csv()` on `None`. – fiveelements Aug 20 '19 at 18:11
  • {'neg': 0.0, 'neu': 0.667, 'pos': 0.333, 'compound': 0.5413} {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0} {'neg': 0.0, 'neu': 0.492, 'pos': 0.508, 'compound': 0.4754} {'neg': 0.0, 'neu': 0.367, 'pos': 0.633, 'compound': 0.7845} {'neg': 0.0, 'neu': 0.691, 'pos': 0.309, 'compound': 0.8004} {'neg': 0.0, 'neu': 0.462, 'pos': 0.538, 'compound': 0.5413} {'neg': 0.0, 'neu': 0.636, 'pos': 0.364, 'compound': 0.7906} –  Aug 20 '19 at 18:22
  • print sentiment returns the sentiment scores –  Aug 20 '19 at 18:22
  • `print(str(aSnt)` prints the sentiment score, does not return it. You have to explicitly add these outputs to a `dict` or `DataFrame` and then return it. You can try `x=print_sentiment_score(your_list)` and then `print(x)`. It will print `None`. Fix this first to return an appropriate object (`dict` or `DataFrame`) and then convert it to `CSV`. – fiveelements Aug 20 '19 at 18:27
  • @fiveelements please help on how to fix that –  Aug 20 '19 at 18:34
  • As I said, you have to return a `dict` or an `array` or a `DataFrame` from `print_sentiment_scores()` function. And then with the returned data, you may convert to CSV. Please see the answer. – fiveelements Aug 20 '19 at 18:38
  • Possible duplicate of [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – G. Anderson Aug 20 '19 at 18:52

2 Answers2

1

you need to fix your print_sentiment_scores like this:

def print_sentiment_scores(alist):
    polarity_scores = []
    for aSentence in alist: 
        aSnt = analyser.polarity_scores(aSentence[0])
        print(str(aSnt))
        polarity_scores += [aSnt]

    return polarity_scores

which will return the following list:

[
    {'neg': 0.0, 'neu': 0.492, 'pos': 0.508, 'compound': 0.4754},
    {'neg': 0.0, 'neu': 0.367, 'pos': 0.633, 'compound': 0.7845},
    {'neg': 0.0, 'neu': 0.691, 'pos': 0.309, 'compound': 0.8004},
    {'neg': 0.0, 'neu': 0.462, 'pos': 0.538, 'compound': 0.5413},
    {'neg': 0.0, 'neu': 0.636, 'pos': 0.364, 'compound': 0.7906}
]

finally, this will generate the required csv:

output_df = DataFrame(print_sentiment_scores(your_list))
output_df.to_csv('some_name.csv')
ruhaib
  • 604
  • 5
  • 12
1

As I mentioned in the comment to the original post, you have to return either a dict or an array or DataFrame from print_sentiment_scores() function.

I suggest the following change to create a DataFrame and return from print_sentiment_scores() function:

def print_sentiment_scores(alist):
    df = pd.DataFrame();
    for aSentence in alist: 
      aSnt = analyser.polarity_scores(aSentence[0])
      df = df.append(aSnt, ignore_index=True)
      print(str(aSnt))

    return df

And then call to_csv() on the returned DataFrame:

df_before = print_sentiment_scores(your_list)
print(df_before.to_csv())
fiveelements
  • 3,649
  • 1
  • 17
  • 16