0

I am using

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()

def parToSent(para):
    mylst = sent_tokenize(para)
    return mylst

def analyzr(txt):
mylst = parToSent(txt)
for i in mylst:
    scr = analyser.polarity_scores(i)
    print(scr, "\n")

and pass some paragraph to the below function:

analyzr(par)

The output comes in this format:

{'neg': 0.081, 'neu': 0.919, 'pos': 0.0, 'compound': -0.296} 

I want to get this output into a dataframe like this:

 neg      neu      pos     compound
 0.081    0.919    0.0     -0.269

how can it be done in a function?

martineau
  • 119,623
  • 25
  • 170
  • 301
loveR
  • 489
  • 4
  • 12
  • but there are many such results, from the for loop. so I guess, we will need to append the DF, right? – loveR Dec 03 '19 at 09:49
  • Does this answer your question? [Convert list of dictionaries to a pandas DataFrame](https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe) – 9769953 Dec 03 '19 at 10:51
  • The suggested duplicate is perhaps not an exact duplicate of your question, but I guess you can fill in the gaps. – 9769953 Dec 03 '19 at 10:51
  • Alternatively, in a (slightly unreadable) one-liner: `df = pandas.DataFrame([analyser.polarity_scores(item) for item in mylst])`. – 9769953 Dec 03 '19 at 10:52

1 Answers1

0

What you can do if you are using pandas is have a column that has the list of words, and then use the function you created applied to the whole df.

For example:

Original df:

    Created at  Text 
0   2020-06-26  foo
1   2020-06-26  bar

If you did the following

df['neg'] =analyser.polarity_scores('Text')['neg'])

The output would be the df with the new column

    Created at  Text neg
0   2020-06-26  foo  0.634
1   2020-06-26  bar  0.77

You can repeat this for the other values you need

kayuzee
  • 75
  • 5